-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
196 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
/etcd_ca.key | ||
/kube_ca.crt | ||
/kube_ca.key | ||
pkg/cloudprovider/providers/aws/mocks/ | ||
|
||
pkg/**/mocks/ | ||
/mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
Copyright 2017 The Keto Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
cloudProviderMocks "github.com/UKHomeOffice/keto/pkg/cloudprovider/mocks" | ||
userdataMocks "github.com/UKHomeOffice/keto/pkg/userdata/mocks" | ||
|
||
"github.com/UKHomeOffice/keto/pkg/model" | ||
) | ||
|
||
const cloudProviderName = "mock" | ||
|
||
type testMock struct { | ||
Provider *cloudProviderMocks.Interface | ||
Clusters *cloudProviderMocks.Clusters | ||
NodePooler *cloudProviderMocks.NodePooler | ||
Node *cloudProviderMocks.Node | ||
UserData *userdataMocks.UserDater | ||
} | ||
|
||
func TestCreateCluster(t *testing.T) { | ||
m, ctrl := makeTestMock() | ||
|
||
persistentIPs := map[string]string{"node0": "1.1.1.1"} | ||
cluster := model.Cluster{ | ||
Name: "foo", | ||
MasterPool: model.MasterPool{NodePool: makeNodePool("foo", "master")}, | ||
} | ||
|
||
m.Clusters.On("GetClusters", cluster.Name).Return([]*model.Cluster{}, nil).Once() | ||
m.Clusters.On("CreateClusterInfra", cluster).Return(nil) | ||
m.Clusters.On("PushAssets", cluster.Name, model.Assets{}).Return(nil) | ||
|
||
// At this point the cluster infra already exists. | ||
m.Clusters.On("GetClusters", cluster.Name).Return([]*model.Cluster{&cluster}, nil).Once() | ||
m.NodePooler.On("GetMasterPools", cluster.Name, "").Return([]*model.MasterPool{}, nil) | ||
m.Clusters.On("GetMasterPersistentIPs", cluster.Name).Return(persistentIPs, nil) | ||
m.Provider.On("ProviderName").Return(cloudProviderName) | ||
|
||
m.UserData.On("RenderMasterCloudConfig", | ||
cloudProviderName, | ||
cluster.Name, | ||
cluster.MasterPool.KubeVersion, | ||
persistentIPs).Return(cluster.MasterPool.UserData, | ||
nil) | ||
|
||
m.NodePooler.On("CreateMasterPool", cluster.MasterPool).Return(nil) | ||
|
||
if err := ctrl.CreateCluster(cluster, model.Assets{}); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
m.Clusters.AssertExpectations(t) | ||
m.UserData.AssertExpectations(t) | ||
m.NodePooler.AssertExpectations(t) | ||
m.Provider.AssertExpectations(t) | ||
} | ||
|
||
func TestCreateClusterAlreadyExists(t *testing.T) { | ||
m, ctrl := makeTestMock() | ||
|
||
cluster := model.Cluster{ | ||
Name: "foo", | ||
MasterPool: model.MasterPool{NodePool: makeNodePool("foo", "master")}, | ||
} | ||
|
||
m.Clusters.On("GetClusters", cluster.Name).Return([]*model.Cluster{&cluster}, nil).Once() | ||
|
||
if err := ctrl.CreateCluster(cluster, model.Assets{}); err != ErrClusterAlreadyExists { | ||
t.Errorf("wrong error; got %q; want %q", err, ErrClusterAlreadyExists) | ||
} | ||
|
||
m.Clusters.AssertExpectations(t) | ||
} | ||
|
||
func TestCreateMasterPoolAlreadyExists(t *testing.T) { | ||
m, ctrl := makeTestMock() | ||
|
||
clusterName := "foo" | ||
p := model.MasterPool{ | ||
NodePool: makeNodePool(clusterName, "master"), | ||
} | ||
|
||
m.Clusters.On("GetClusters", clusterName).Return([]*model.Cluster{&model.Cluster{Name: clusterName}}, nil).Once() | ||
m.NodePooler.On("GetMasterPools", clusterName, "").Return([]*model.MasterPool{&p}, nil) | ||
|
||
if err := ctrl.CreateMasterPool(p); err != ErrMasterPoolAlreadyExists { | ||
t.Errorf("wrong error; got %q; want %q", err, ErrMasterPoolAlreadyExists) | ||
} | ||
|
||
m.Clusters.AssertExpectations(t) | ||
m.NodePooler.AssertExpectations(t) | ||
} | ||
|
||
func TestDeleteCluster(t *testing.T) { | ||
m, ctrl := makeTestMock() | ||
m.Clusters.On("DeleteCluster", "foo").Return(nil) | ||
|
||
if err := ctrl.DeleteCluster("foo"); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
m.Clusters.AssertExpectations(t) | ||
} | ||
|
||
func makeTestMock() (*testMock, *Controller) { | ||
m := &testMock{ | ||
Provider: &cloudProviderMocks.Interface{}, | ||
Clusters: &cloudProviderMocks.Clusters{}, | ||
NodePooler: &cloudProviderMocks.NodePooler{}, | ||
Node: &cloudProviderMocks.Node{}, | ||
UserData: &userdataMocks.UserDater{}, | ||
} | ||
|
||
m.Provider.On("Clusters").Return(m.Clusters, true) | ||
m.Provider.On("NodePooler").Return(m.NodePooler, true) | ||
|
||
ctrl := New(Config{Cloud: m.Provider, UserData: m.UserData}) | ||
return m, ctrl | ||
} | ||
|
||
func makeNodePool(clusterName, name string) model.NodePool { | ||
meta := model.ResourceMeta{ | ||
Name: name, | ||
ClusterName: clusterName, | ||
} | ||
|
||
spec := model.NodePoolSpec{ | ||
KubeVersion: "v1.7.0", | ||
MachineType: "tiny", | ||
CoreOSVersion: "v1", | ||
SSHKey: "s3cr3tkey", | ||
DiskSize: 10, | ||
Size: 1, | ||
Networks: []string{"network0", "network1"}, | ||
UserData: []byte("mocked userdata"), | ||
} | ||
|
||
return model.NodePool{ | ||
ResourceMeta: meta, | ||
NodePoolSpec: spec, | ||
} | ||
} |