From 0394cc19de4032fc0db7bcaae96e39e250bd2f47 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Sat, 11 May 2019 14:26:49 +0100 Subject: [PATCH] fix tests for GCP cloud config, make output useful --- bosh/internal/boshcli/gcp_test.go | 140 +++++++----------- .../fixtures/gcp_cloud_config_full.yml | 130 ---------------- .../fixtures/gcp_cloud_config_no_spot.yml | 28 ++-- .../fixtures/gcp_cloud_config_spot.yml | 28 ++-- 4 files changed, 84 insertions(+), 242 deletions(-) delete mode 100644 bosh/internal/fixtures/gcp_cloud_config_full.yml diff --git a/bosh/internal/boshcli/gcp_test.go b/bosh/internal/boshcli/gcp_test.go index 2480ddbc0..76d4f157b 100644 --- a/bosh/internal/boshcli/gcp_test.go +++ b/bosh/internal/boshcli/gcp_test.go @@ -1,105 +1,73 @@ package boshcli import ( - "fmt" "github.com/EngineerBetter/control-tower/resource" + "github.com/onsi/gomega/format" "io/ioutil" "testing" "text/template" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" ) -func TestGCPEnvironment_ConfigureDirectorCloudConfig(t *testing.T) { +func getFixture(filename string) string { + contents, _ := ioutil.ReadFile(filename) + return string(contents) +} - fullTemplateParams := GCPEnvironment{ - Zone: "zone", - PublicSubnetwork: "public_subnetwork", - PrivateSubnetwork: "private_subnetwork", - Spot: false, - Network: "network", - PublicCIDR: "public_cidr", - PublicCIDRGateway: "public_cidr_gateway", - PublicCIDRStatic: "public_cidr_static", - PublicCIDRReserved: "public_cidr_reserved", - PrivateCIDR: "private_cidr", - PrivateCIDRGateway: "private_cidr_gateway", - PrivateCIDRReserved: "private_cidr_reserved", - } +var _ = Describe("GCPEnvironment", func() { + Describe("ConfigureDirectorCloudConfig", func() { + var expected string + var environment GCPEnvironment - getFixture := func(f string) string { - contents, _ := ioutil.ReadFile(f) - return string(contents) - } + BeforeEach(func() { + environment = GCPEnvironment{ + Zone: "zone", + PublicSubnetwork: "public_subnetwork", + PrivateSubnetwork: "private_subnetwork", + Spot: false, + Network: "network", + PublicCIDR: "public_cidr", + PublicCIDRGateway: "public_cidr_gateway", + PublicCIDRStatic: "public_cidr_static", + PublicCIDRReserved: "public_cidr_reserved", + PrivateCIDR: "private_cidr", + PrivateCIDRGateway: "private_cidr_gateway", + PrivateCIDRReserved: "private_cidr_reserved", + } - tests := []struct { - name string - fields GCPEnvironment - want string - wantErr bool - init func(GCPEnvironment) GCPEnvironment - validate func(string, string) (bool, string) - }{ - { - name: "Success- template rendered", - fields: fullTemplateParams, - want: getFixture("../fixtures/gcp_cloud_config_full.yml"), - wantErr: false, - init: func(e GCPEnvironment) GCPEnvironment { - return e - }, - validate: func(a, b string) (bool, string) { - return a == b, fmt.Sprintf("basic rendering expected to work") - }, - }, - { - name: "Success- spot instance rendered", - fields: fullTemplateParams, - want: getFixture("../fixtures/gcp_cloud_config_spot.yml"), - wantErr: false, - init: func(e GCPEnvironment) GCPEnvironment { - n := e - n.Spot = true - return n - }, - validate: func(a, b string) (bool, string) { - return a == b, fmt.Sprintf("templating failed while rendering without spots") - }, - }, + format.TruncatedDiff = false + }) - { - name: "Success- running with no spot", - fields: fullTemplateParams, - want: getFixture("../fixtures/gcp_cloud_config_no_spot.yml"), - wantErr: false, - init: func(e GCPEnvironment) GCPEnvironment { - n := e - n.Spot = false - return n - }, - validate: func(a, b string) (bool, string) { - return a == b, fmt.Sprintf("templating failed while rendering without spots") - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - e := tt.init(tt.fields) - got, err := e.ConfigureDirectorCloudConfig() - if (err != nil) != tt.wantErr { - t.Errorf("Environment.ConfigureDirectorCloudConfig()\nerror expected: %v\nreceived error: %v", tt.wantErr, err) - return - } - passed, message := tt.validate(got, tt.want) - if !passed { - t.Errorf(message) - } + Context("when spot instances are not requested", func() { + BeforeEach(func() { + expected = getFixture("../fixtures/gcp_cloud_config_no_spot.yml") + }) + + It("renders the expected YAML", func() { + actual, err := environment.ConfigureDirectorCloudConfig() + Expect(err).ToNot(HaveOccurred()) + Expect(actual).To(Equal(expected)) + }) }) - } -} + + Context("when spot instances are requested", func() { + BeforeEach(func() { + expected = getFixture("../fixtures/gcp_cloud_config_spot.yml") + environment.Spot = true + }) + + It("renders the expected YAML", func() { + actual, err := environment.ConfigureDirectorCloudConfig() + Expect(err).ToNot(HaveOccurred()) + Expect(actual).To(Equal(expected)) + }) + }) + }) +}) func TestGCPEnvironment_ConfigureConcourseStemcell(t *testing.T) { - type args struct { - versions string - } tests := []struct { name string want string diff --git a/bosh/internal/fixtures/gcp_cloud_config_full.yml b/bosh/internal/fixtures/gcp_cloud_config_full.yml deleted file mode 100644 index 780ec48c5..000000000 --- a/bosh/internal/fixtures/gcp_cloud_config_full.yml +++ /dev/null @@ -1,130 +0,0 @@ ---- -azs: -- name: z1 - cloud_properties: - zone: zone - -vm_types: -- name: concourse-web-small - cloud_properties: - machine_type: n1-standard-1 - root_disk_size_gb: 20 - root_disk_type: pd-ssd - -- name: concourse-web-medium - cloud_properties: - machine_type: n1-standard-2 - root_disk_size_gb: 20 - root_disk_type: pd-ssd - -- name: concourse-web-large - cloud_properties: - machine_type: n1-standard-4 - root_disk_size_gb: 20 - root_disk_type: pd-ssd - -- name: concourse-web-xlarge - cloud_properties: - machine_type: n1-standard-8 - root_disk_size_gb: 20 - root_disk_type: pd-ssd - -- name: concourse-web-2xlarge - cloud_properties: - machine_type: n1-standard-16 - root_disk_size_gb: 20 - root_disk_type: pd-ssd - -- name: concourse-medium - cloud_properties: - machine_type: n1-standard-1 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-large - cloud_properties: - machine_type: n1-standard-2 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-xlarge - cloud_properties: - machine_type: n1-standard-4 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-2xlarge - cloud_properties: - machine_type: n1-standard-8 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-4xlarge - cloud_properties: - machine_type: n1-standard-16 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-10xlarge - cloud_properties: - machine_type: n1-standard-32 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: concourse-16xlarge - cloud_properties: - machine_type: n1-standard-64 - root_disk_size_gb: 200 - root_disk_type: pd-ssd - -- name: compilation - cloud_properties: - machine_type: n1-standard-2 - root_disk_size_gb: 5 - root_disk_type: pd-ssd - -disk_types: -- name: default - disk_size: 50_000 - cloud_properties: - type: pd-ssd -- name: large - disk_size: 200_000 - cloud_properties: - type: pd-ssd - -networks: -- name: public - type: manual - subnets: - - range: public_cidr - gateway: public_cidr_gateway - az: z1 - static: public_cidr_static - reserved: public_cidr_reserved - cloud_properties: - network_name: network - subnetwork_name: public_subnetwork -- name: private - type: manual - subnets: - - range: private_cidr - gateway: private_cidr_gateway - az: z1 - reserved: private_cidr_reserved - cloud_properties: - network_name: network - subnetwork_name: private_subnetwork - tags: [no-ip] -- name: vip - type: vip - -vm_extensions: -- name: atc - -compilation: - workers: 5 - reuse_compilation_vms: true - az: z1 - vm_type: compilation - network: private diff --git a/bosh/internal/fixtures/gcp_cloud_config_no_spot.yml b/bosh/internal/fixtures/gcp_cloud_config_no_spot.yml index 780ec48c5..241b9ff6f 100644 --- a/bosh/internal/fixtures/gcp_cloud_config_no_spot.yml +++ b/bosh/internal/fixtures/gcp_cloud_config_no_spot.yml @@ -9,79 +9,81 @@ vm_types: cloud_properties: machine_type: n1-standard-1 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : &common_properties + service_scopes: [cloud-platform] + root_disk_type: pd-ssd - name: concourse-web-medium cloud_properties: machine_type: n1-standard-2 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-large cloud_properties: machine_type: n1-standard-4 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-xlarge cloud_properties: machine_type: n1-standard-8 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-2xlarge cloud_properties: machine_type: n1-standard-16 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-medium cloud_properties: machine_type: n1-standard-1 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-large cloud_properties: machine_type: n1-standard-2 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-xlarge cloud_properties: machine_type: n1-standard-4 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-2xlarge cloud_properties: machine_type: n1-standard-8 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-4xlarge cloud_properties: machine_type: n1-standard-16 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-10xlarge cloud_properties: machine_type: n1-standard-32 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-16xlarge cloud_properties: machine_type: n1-standard-64 root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: compilation cloud_properties: machine_type: n1-standard-2 root_disk_size_gb: 5 - root_disk_type: pd-ssd + << : *common_properties disk_types: - name: default diff --git a/bosh/internal/fixtures/gcp_cloud_config_spot.yml b/bosh/internal/fixtures/gcp_cloud_config_spot.yml index b84b8a213..ae1c9385c 100644 --- a/bosh/internal/fixtures/gcp_cloud_config_spot.yml +++ b/bosh/internal/fixtures/gcp_cloud_config_spot.yml @@ -9,87 +9,89 @@ vm_types: cloud_properties: machine_type: n1-standard-1 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : &common_properties + service_scopes: [cloud-platform] + root_disk_type: pd-ssd - name: concourse-web-medium cloud_properties: machine_type: n1-standard-2 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-large cloud_properties: machine_type: n1-standard-4 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-xlarge cloud_properties: machine_type: n1-standard-8 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-web-2xlarge cloud_properties: machine_type: n1-standard-16 root_disk_size_gb: 20 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-medium cloud_properties: machine_type: n1-standard-1 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-large cloud_properties: machine_type: n1-standard-2 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-xlarge cloud_properties: machine_type: n1-standard-4 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-2xlarge cloud_properties: machine_type: n1-standard-8 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-4xlarge cloud_properties: machine_type: n1-standard-16 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-10xlarge cloud_properties: machine_type: n1-standard-32 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: concourse-16xlarge cloud_properties: machine_type: n1-standard-64 preemptible: true # root_disk_size_gb: 200 - root_disk_type: pd-ssd + << : *common_properties - name: compilation cloud_properties: machine_type: n1-standard-2 preemptible: true # root_disk_size_gb: 5 - root_disk_type: pd-ssd + << : *common_properties disk_types: - name: default