From 2f256b9d3976578f94bf90ed249e0279457958ec Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Tue, 20 Dec 2016 11:36:29 -0500 Subject: [PATCH] Add tests for docker nodeup side --- nodeup/pkg/model/docker_test.go | 98 +++++++++++++++++++ .../dockerbuilder/docker_1.12.1/cluster.yaml | 39 ++++++++ .../dockerbuilder/docker_1.12.1/tasks.yaml | 18 ++++ .../tests/dockerbuilder/simple/cluster.yaml | 37 +++++++ .../tests/dockerbuilder/simple/tasks.yaml | 18 ++++ upup/pkg/fi/nodeup/nodetasks/file.go | 8 +- upup/pkg/fi/nodeup/nodetasks/package.go | 10 +- 7 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 nodeup/pkg/model/docker_test.go create mode 100644 nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/cluster.yaml create mode 100644 nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/tasks.yaml create mode 100644 nodeup/pkg/model/tests/dockerbuilder/simple/cluster.yaml create mode 100644 nodeup/pkg/model/tests/dockerbuilder/simple/tasks.yaml diff --git a/nodeup/pkg/model/docker_test.go b/nodeup/pkg/model/docker_test.go new file mode 100644 index 0000000000..7712406b1b --- /dev/null +++ b/nodeup/pkg/model/docker_test.go @@ -0,0 +1,98 @@ +/* +Copyright 2016 The Kubernetes 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 model + +import ( + "io/ioutil" + "k8s.io/kops/pkg/apis/kops" + "k8s.io/kops/pkg/diff" + "k8s.io/kops/upup/pkg/fi" + "path" + "sort" + "strings" + "testing" + + // Register our APIs + _ "k8s.io/kops/pkg/apis/kops/install" +) + +func TestDockerBuilder(t *testing.T) { + runDockerBuilderTest(t, "simple") + runDockerBuilderTest(t, "docker_1.12.1") +} + +func runDockerBuilderTest(t *testing.T, key string) { + basedir := path.Join("tests/dockerbuilder/", key) + + clusterYamlPath := path.Join(basedir, "cluster.yaml") + clusterYaml, err := ioutil.ReadFile(clusterYamlPath) + if err != nil { + t.Fatalf("error reading cluster yaml file %q: %v", clusterYamlPath, err) + } + obj, _, err := kops.ParseVersionedYaml(clusterYaml) + if err != nil { + t.Fatalf("error parsing cluster yaml %q: %v", clusterYamlPath, err) + } + cluster := obj.(*kops.Cluster) + + context := &fi.ModelBuilderContext{ + Tasks: make(map[string]fi.Task), + } + nodeUpModelContext := &NodeupModelContext{ + Cluster: cluster, + Architecture: "amd64", + Distribution: DistributionXenial, + } + + builder := DockerBuilder{NodeupModelContext: nodeUpModelContext} + + err = builder.Build(context) + if err != nil { + t.Fatalf("error from DockerBuilder Build: %v", err) + } + + var keys []string + for key := range context.Tasks { + keys = append(keys, key) + } + sort.Strings(keys) + + var yamls []string + for _, key := range keys { + task := context.Tasks[key] + yaml, err := kops.ToRawYaml(task) + if err != nil { + t.Fatalf("error serializing task: %v", err) + } + yamls = append(yamls, strings.TrimSpace(string(yaml))) + } + + actualTasksYaml := strings.Join(yamls, "\n---\n") + + tasksYamlPath := path.Join(basedir, "tasks.yaml") + expectedTasksYaml, err := ioutil.ReadFile(tasksYamlPath) + if err != nil { + t.Fatalf("error reading file %q: %v", tasksYamlPath, err) + } + + if strings.TrimSpace(string(expectedTasksYaml)) != strings.TrimSpace(actualTasksYaml) { + diffString := diff.FormatDiff(string(expectedTasksYaml), actualTasksYaml) + t.Logf("diff:\n%s\n", diffString) + + t.Fatalf("tasks differed from expected for test %q", key) + } +} diff --git a/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/cluster.yaml b/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/cluster.yaml new file mode 100644 index 0000000000..39d3c3c02a --- /dev/null +++ b/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/cluster.yaml @@ -0,0 +1,39 @@ +apiVersion: kops/v1alpha2 +kind: Cluster +metadata: + creationTimestamp: "2016-12-10T22:42:27Z" + name: minimal.example.com +spec: + kubernetesApiAccess: + - 0.0.0.0/0 + channel: stable + cloudProvider: aws + configBase: memfs://clusters.example.com/minimal.example.com + etcdClusters: + - etcdMembers: + - instanceGroup: master-us-test-1a + name: master-us-test-1a + name: main + - etcdMembers: + - instanceGroup: master-us-test-1a + name: master-us-test-1a + name: events + kubernetesVersion: v1.4.6 + masterInternalName: api.internal.minimal.example.com + masterPublicName: api.minimal.example.com + networkCIDR: 172.20.0.0/16 + networking: + kubenet: {} + nonMasqueradeCIDR: 100.64.0.0/10 + sshAccess: + - 0.0.0.0/0 + topology: + masters: public + nodes: public + subnets: + - cidr: 172.20.32.0/19 + name: us-test-1a + type: Public + zone: us-test-1a + docker: + version: 1.12.1 \ No newline at end of file diff --git a/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/tasks.yaml b/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/tasks.yaml new file mode 100644 index 0000000000..b861fd8313 --- /dev/null +++ b/nodeup/pkg/model/tests/dockerbuilder/docker_1.12.1/tasks.yaml @@ -0,0 +1,18 @@ +contents: {} +path: /usr/share/doc/docker/apache.txt +type: file +--- +Name: bridge-utils +--- +Name: docker-engine +hash: 30f7840704361673db2b62f25b6038628184b056 +preventStart: true +source: http://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_1.12.1-0~xenial_amd64.deb +version: 1.12.1-0~xenial +--- +Name: libapparmor1 +--- +Name: libltdl7 +--- +Name: perl + diff --git a/nodeup/pkg/model/tests/dockerbuilder/simple/cluster.yaml b/nodeup/pkg/model/tests/dockerbuilder/simple/cluster.yaml new file mode 100644 index 0000000000..33674de3c2 --- /dev/null +++ b/nodeup/pkg/model/tests/dockerbuilder/simple/cluster.yaml @@ -0,0 +1,37 @@ +apiVersion: kops/v1alpha2 +kind: Cluster +metadata: + creationTimestamp: "2016-12-10T22:42:27Z" + name: minimal.example.com +spec: + kubernetesApiAccess: + - 0.0.0.0/0 + channel: stable + cloudProvider: aws + configBase: memfs://clusters.example.com/minimal.example.com + etcdClusters: + - etcdMembers: + - instanceGroup: master-us-test-1a + name: master-us-test-1a + name: main + - etcdMembers: + - instanceGroup: master-us-test-1a + name: master-us-test-1a + name: events + kubernetesVersion: v1.4.6 + masterInternalName: api.internal.minimal.example.com + masterPublicName: api.minimal.example.com + networkCIDR: 172.20.0.0/16 + networking: + kubenet: {} + nonMasqueradeCIDR: 100.64.0.0/10 + sshAccess: + - 0.0.0.0/0 + topology: + masters: public + nodes: public + subnets: + - cidr: 172.20.32.0/19 + name: us-test-1a + type: Public + zone: us-test-1a diff --git a/nodeup/pkg/model/tests/dockerbuilder/simple/tasks.yaml b/nodeup/pkg/model/tests/dockerbuilder/simple/tasks.yaml new file mode 100644 index 0000000000..322c52ef80 --- /dev/null +++ b/nodeup/pkg/model/tests/dockerbuilder/simple/tasks.yaml @@ -0,0 +1,18 @@ +contents: {} +path: /usr/share/doc/docker/apache.txt +type: file +--- +Name: bridge-utils +--- +Name: docker-engine +hash: b758fc88346a1e5eebf7408b0d0c99f4f134166c +preventStart: true +source: http://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_1.12.3-0~xenial_amd64.deb +version: 1.12.3-0~xenial +--- +Name: libapparmor1 +--- +Name: libltdl7 +--- +Name: perl + diff --git a/upup/pkg/fi/nodeup/nodetasks/file.go b/upup/pkg/fi/nodeup/nodetasks/file.go index 12d244d2fa..40027eb2ef 100644 --- a/upup/pkg/fi/nodeup/nodetasks/file.go +++ b/upup/pkg/fi/nodeup/nodetasks/file.go @@ -36,11 +36,11 @@ const FileType_Directory = "directory" const FileType_File = "file" type File struct { - Path string - Contents fi.Resource + Path string `json:"path,omitempty"` + Contents fi.Resource `json:"contents,omitempty"` - Mode *string `json:"mode"` - IfNotExists bool `json:"ifNotExists"` + Mode *string `json:"mode,omitempty"` + IfNotExists bool `json:"ifNotExists,omitempty"` OnChangeExecute []string `json:"onChangeExecute,omitempty"` diff --git a/upup/pkg/fi/nodeup/nodetasks/package.go b/upup/pkg/fi/nodeup/nodetasks/package.go index fcefb78254..1f943fb257 100644 --- a/upup/pkg/fi/nodeup/nodetasks/package.go +++ b/upup/pkg/fi/nodeup/nodetasks/package.go @@ -36,13 +36,13 @@ import ( type Package struct { Name string - Version *string `json:"version"` - Source *string `json:"source"` - Hash *string `json:"hash"` - PreventStart *bool `json:"preventStart"` + Version *string `json:"version,omitempty"` + Source *string `json:"source,omitempty"` + Hash *string `json:"hash,omitempty"` + PreventStart *bool `json:"preventStart,omitempty"` // Healthy is true if the package installation did not fail - Healthy *bool `json:"healthy"` + Healthy *bool `json:"healthy,omitempty"` } const (