Skip to content

Commit

Permalink
Integration tests for phases - iam works - others are WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislovecnm committed Oct 12, 2017
1 parent f00ee27 commit eeafe64
Show file tree
Hide file tree
Showing 8 changed files with 1,032 additions and 11 deletions.
114 changes: 103 additions & 11 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/ghodss/yaml"
"golang.org/x/crypto/ssh"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
)

Expand Down Expand Up @@ -132,12 +133,49 @@ func TestSharedVPC(t *testing.T) {
runTestAWS(t, "sharedvpc.example.com", "shared_vpc", "v1alpha2", false, 1)
}

func runTest(t *testing.T, h *testutils.IntegrationTestHarness, clusterName string, srcDir string, version string, private bool, zones int, expectedFilenames []string) {
// TestPhaseNetwork tests the output of tf for the network phase
func TestPhaseNetwork(t *testing.T) {
t.Skip("unable to pass test w/o removing elb stuff")
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseNetwork)
}

// TestPhaseIAM tests the output of tf for the iam phase
func TestPhaseIAM(t *testing.T) {
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseIAM)
}

// TestPhaseCluster tests the output of tf for the cluster phase
func TestPhaseCluster(t *testing.T) {
// TODO fix tf for phase, and allow override on validation
t.Skip("unable to test w/o allowing failed validation")
runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.PhaseCluster)
}

// TestPhaseCluster tests the output of tf for the security group phase
func TestPhaseSecurityGroup(t *testing.T) {
t.Skip("unable to test until phase is created")
// TODO fix tf for phase, and allow override on validation
// runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.SecurityGroups)
}

// TestPhaseCluster tests the output of tf for the loadbalancer phase
func TestPhaseLoadBalancers(t *testing.T) {
t.Skip("unable to test until phase is created")
// TODO
// runTestPhase(t, "privateweave.example.com", "lifecycle_phases", "v1alpha2", true, 1, cloudup.LoadBalancers)
}

func runTest(t *testing.T, h *testutils.IntegrationTestHarness, clusterName string, srcDir string, version string, private bool, zones int, expectedFilenames []string, tfFileName string, phase *cloudup.Phase) {
var stdout bytes.Buffer

srcDir = updateClusterTestBase + srcDir
inputYAML := "in-" + version + ".yaml"
expectedTFPath := "kubernetes.tf"
testDataTFPath := "kubernetes.tf"
actualTFPath := "kubernetes.tf"

if tfFileName != "" {
testDataTFPath = tfFileName
}

factoryOptions := &util.FactoryOptions{}
factoryOptions.RegistryPath = "memfs://tests"
Expand Down Expand Up @@ -172,6 +210,9 @@ func runTest(t *testing.T, h *testutils.IntegrationTestHarness, clusterName stri
options.Target = "terraform"
options.OutDir = path.Join(h.TempDir, "out")
options.MaxTaskDuration = 30 * time.Second
if phase != nil {
options.Phase = string(*phase)
}

// We don't test it here, and it adds a dependency on kubectl
options.CreateKubecfg = false
Expand All @@ -196,22 +237,27 @@ func runTest(t *testing.T, h *testutils.IntegrationTestHarness, clusterName stri
sort.Strings(fileNames)

actualFilenames := strings.Join(fileNames, ",")
expectedFilenames := "data,kubernetes.tf"
if actualFilenames != expectedFilenames {
t.Fatalf("unexpected files. actual=%q, expected=%q", actualFilenames, expectedFilenames)
expected := "kubernetes.tf"

if len(expectedFilenames) > 0 {
expected = "data,kubernetes.tf"
}

if actualFilenames != expected {
t.Fatalf("unexpected files. actual=%q, expected=%q, test=%q", actualFilenames, expected, testDataTFPath)
}

actualTF, err := ioutil.ReadFile(path.Join(h.TempDir, "out", "kubernetes.tf"))
actualTF, err := ioutil.ReadFile(path.Join(h.TempDir, "out", actualTFPath))
if err != nil {
t.Fatalf("unexpected error reading actual terraform output: %v", err)
}
expectedTF, err := ioutil.ReadFile(path.Join(srcDir, expectedTFPath))
testDataTF, err := ioutil.ReadFile(path.Join(srcDir, testDataTFPath))
if err != nil {
t.Fatalf("unexpected error reading expected terraform output: %v", err)
}

if !bytes.Equal(actualTF, expectedTF) {
diffString := diff.FormatDiff(string(expectedTF), string(actualTF))
if !bytes.Equal(actualTF, testDataTF) {
diffString := diff.FormatDiff(string(testDataTF), string(actualTF))
t.Logf("diff:\n%s\n", diffString)

t.Fatalf("terraform output differed from expected")
Expand Down Expand Up @@ -270,7 +316,53 @@ func runTestAWS(t *testing.T, clusterName string, srcDir string, version string,
// "aws_launch_configuration_bastions." + clusterName + "_user_data",
}...)
}
runTest(t, h, clusterName, srcDir, version, private, zones, expectedFilenames)
runTest(t, h, clusterName, srcDir, version, private, zones, expectedFilenames, "", nil)
}

func runTestPhase(t *testing.T, clusterName string, srcDir string, version string, private bool, zones int, phase cloudup.Phase) {
h := testutils.NewIntegrationTestHarness(t)
defer h.Close()

h.MockKopsVersion("1.7.0")
h.SetupMockAWS()
phaseName := string(phase)
if phaseName == "" {
t.Fatalf("phase must be set")
}
tfFileName := phaseName + "-kubernetes.tf"

expectedFilenames := []string{}

if phase == cloudup.PhaseIAM {
expectedFilenames = []string{
"aws_iam_role_masters." + clusterName + "_policy",
"aws_iam_role_nodes." + clusterName + "_policy",
"aws_iam_role_policy_masters." + clusterName + "_policy",
"aws_iam_role_policy_nodes." + clusterName + "_policy",
"aws_key_pair_kubernetes." + clusterName + "-c4a6ed9aa889b9e2c39cd663eb9c7157_public_key",
}
if private {
expectedFilenames = append(expectedFilenames, []string{
"aws_iam_role_bastions." + clusterName + "_policy",
"aws_iam_role_policy_bastions." + clusterName + "_policy",

// bastions don't have any userdata
// "aws_launch_configuration_bastions." + clusterName + "_user_data",
}...)
}
} else if phase == cloudup.PhaseCluster {
expectedFilenames = []string{
"aws_launch_configuration_nodes." + clusterName + "_user_data",
}

for i := 0; i < zones; i++ {
zone := "us-test-1" + string([]byte{byte('a') + byte(i)})
s := "aws_launch_configuration_master-" + zone + ".masters." + clusterName + "_user_data"
expectedFilenames = append(expectedFilenames, s)
}
}

runTest(t, h, clusterName, srcDir, version, private, zones, expectedFilenames, tfFileName, &phase)
}

func runTestGCE(t *testing.T, clusterName string, srcDir string, version string, private bool, zones int) {
Expand All @@ -295,7 +387,7 @@ func runTestGCE(t *testing.T, clusterName string, srcDir string, version string,
expectedFilenames = append(expectedFilenames, prefix+"startup-script")
}

runTest(t, h, clusterName, srcDir, version, private, zones, expectedFilenames)
runTest(t, h, clusterName, srcDir, version, private, zones, expectedFilenames, "", nil)
}

func runTestCloudformation(t *testing.T, clusterName string, srcDir string, version string, private bool) {
Expand Down
Loading

0 comments on commit eeafe64

Please sign in to comment.