Skip to content

Commit

Permalink
Merge pull request #5547 from justinsb/etcd_manager_tests
Browse files Browse the repository at this point in the history
Add test for etcd-manager output
  • Loading branch information
justinsb authored Sep 22, 2018
2 parents b7d7333 + 7cf432f commit 666e290
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 92 deletions.
5 changes: 1 addition & 4 deletions nodeup/pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@ go_test(
deps = [
"//nodeup/pkg/distros:go_default_library",
"//pkg/apis/kops:go_default_library",
"//pkg/apis/kops/v1alpha2:go_default_library",
"//pkg/diff:go_default_library",
"//pkg/flagbuilder:go_default_library",
"//pkg/kopscodecs:go_default_library",
"//pkg/testutils:go_default_library",
"//upup/pkg/fi:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
],
)
5 changes: 3 additions & 2 deletions nodeup/pkg/model/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/pkg/testutils"
"k8s.io/kops/upup/pkg/fi"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func TestDockerBuilder_BuildFlags(t *testing.T) {
func runDockerBuilderTest(t *testing.T, key string) {
basedir := path.Join("tests/dockerbuilder/", key)

nodeUpModelContext, err := LoadModel(basedir)
nodeUpModelContext, err := BuildNodeupModelContext(basedir)
if err != nil {
t.Fatalf("error parsing cluster yaml %q: %v", basedir, err)
return
Expand All @@ -115,5 +116,5 @@ func runDockerBuilderTest(t *testing.T, key string) {
return
}

ValidateTasks(t, basedir, context)
testutils.ValidateTasks(t, basedir, context)
}
105 changes: 21 additions & 84 deletions nodeup/pkg/model/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@ limitations under the License.
package model

import (
"bytes"
"io/ioutil"
"path"
"sort"
"strings"
"testing"

"fmt"
"testing"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kops/nodeup/pkg/distros"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/v1alpha2"
"k8s.io/kops/pkg/diff"
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kops/pkg/testutils"
"k8s.io/kops/upup/pkg/fi"
)

Expand Down Expand Up @@ -170,7 +161,7 @@ func Test_RunKubeletBuilder(t *testing.T) {
context := &fi.ModelBuilderContext{
Tasks: make(map[string]fi.Task),
}
nodeUpModelContext, err := LoadModel(basedir)
nodeUpModelContext, err := BuildNodeupModelContext(basedir)
if err != nil {
t.Fatalf("error loading model %q: %v", basedir, err)
return
Expand All @@ -191,90 +182,36 @@ func Test_RunKubeletBuilder(t *testing.T) {
}
context.AddTask(fileTask)

ValidateTasks(t, basedir, context)
testutils.ValidateTasks(t, basedir, context)
}

func LoadModel(basedir string) (*NodeupModelContext, error) {
clusterYamlPath := path.Join(basedir, "cluster.yaml")
clusterYaml, err := ioutil.ReadFile(clusterYamlPath)
func BuildNodeupModelContext(basedir string) (*NodeupModelContext, error) {
model, err := testutils.LoadModel(basedir)
if err != nil {
return nil, fmt.Errorf("error reading cluster yaml file %q: %v", clusterYamlPath, err)
return nil, err
}

var cluster *kops.Cluster
var instanceGroup *kops.InstanceGroup

// Codecs provides access to encoding and decoding for the scheme
codecs := kopscodecs.Codecs

codec := codecs.UniversalDecoder(kops.SchemeGroupVersion)

sections := bytes.Split(clusterYaml, []byte("\n---\n"))
for _, section := range sections {
defaults := &schema.GroupVersionKind{
Group: v1alpha2.SchemeGroupVersion.Group,
Version: v1alpha2.SchemeGroupVersion.Version,
}
o, gvk, err := codec.Decode(section, defaults, nil)
if err != nil {
return nil, fmt.Errorf("error parsing file %v", err)
}

switch v := o.(type) {
case *kops.Cluster:
cluster = v
case *kops.InstanceGroup:
instanceGroup = v
default:
return nil, fmt.Errorf("Unhandled kind %q", gvk)
}
if model.Cluster == nil {
return nil, fmt.Errorf("no cluster found in %s", basedir)
}

nodeUpModelContext := &NodeupModelContext{
Cluster: cluster,
Architecture: "amd64",
Distribution: distros.DistributionXenial,
InstanceGroup: instanceGroup,
}
if err := nodeUpModelContext.Init(); err != nil {
return nil, err
Cluster: model.Cluster,
Architecture: "amd64",
Distribution: distros.DistributionXenial,
}

return nodeUpModelContext, nil
}

func ValidateTasks(t *testing.T, basedir string, context *fi.ModelBuilderContext) {
var keys []string
for key := range context.Tasks {
keys = append(keys, key)
if len(model.InstanceGroups) == 0 {
// We tolerate this - not all tests need an instance group
} else if len(model.InstanceGroups) == 1 {
nodeUpModelContext.InstanceGroup = model.InstanceGroups[0]
} else {
return nil, fmt.Errorf("unexpected number of instance groups in %s, found %d", basedir, len(model.InstanceGroups))
}
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")
expectedTasksYamlBytes, err := ioutil.ReadFile(tasksYamlPath)
if err != nil {
t.Fatalf("error reading file %q: %v", tasksYamlPath, err)
if err := nodeUpModelContext.Init(); err != nil {
return nil, err
}

actualTasksYaml = strings.TrimSpace(actualTasksYaml)
expectedTasksYaml := strings.TrimSpace(string(expectedTasksYamlBytes))

if expectedTasksYaml != actualTasksYaml {
diffString := diff.FormatDiff(expectedTasksYaml, actualTasksYaml)
t.Logf("diff:\n%s\n", diffString)

t.Fatalf("tasks differed from expected for test %q", basedir)
}
return nodeUpModelContext, nil
}
15 changes: 14 additions & 1 deletion pkg/model/components/etcdmanager/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -28,3 +28,16 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["model_test.go"],
data = glob(["tests/**"]), #keep
embed = [":go_default_library"],
deps = [
"//pkg/assets:go_default_library",
"//pkg/model:go_default_library",
"//pkg/testutils:go_default_library",
"//upup/pkg/fi:go_default_library",
],
)
74 changes: 74 additions & 0 deletions pkg/model/components/etcdmanager/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2018 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 etcdmanager

import (
"fmt"
"testing"

"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/testutils"
"k8s.io/kops/upup/pkg/fi"
)

func Test_RunEtcdManagerBuilder(t *testing.T) {
basedir := "tests/minimal"

context := &fi.ModelBuilderContext{
Tasks: make(map[string]fi.Task),
}
kopsModelContext, err := LoadKopsModelContext(basedir)
if err != nil {
t.Fatalf("error loading model %q: %v", basedir, err)
return
}

builder := EtcdManagerBuilder{
KopsModelContext: kopsModelContext,
AssetBuilder: assets.NewAssetBuilder(kopsModelContext.Cluster, ""),
}

if err := builder.Build(context); err != nil {
t.Fatalf("error from Build: %v", err)
return
}

testutils.ValidateTasks(t, basedir, context)
}

func LoadKopsModelContext(basedir string) (*model.KopsModelContext, error) {
spec, err := testutils.LoadModel(basedir)
if err != nil {
return nil, err
}

if spec.Cluster == nil {
return nil, fmt.Errorf("no cluster found in %s", basedir)
}

if len(spec.InstanceGroups) == 0 {
return nil, fmt.Errorf("no instance groups found in %s", basedir)
}

kopsContext := &model.KopsModelContext{
Cluster: spec.Cluster,
InstanceGroups: spec.InstanceGroups,
}

return kopsContext, nil
}
85 changes: 85 additions & 0 deletions pkg/model/components/etcdmanager/tests/minimal/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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: us-test-1a
name: main
backups:
backupStore: memfs://clusters.example.com/minimal.example.com/backups/etcd-main
manager:
image: kopeio/etcd-manager:latest
- etcdMembers:
- instanceGroup: master-us-test-1a
name: us-test-1a
name: events
backups:
backupStore: memfs://clusters.example.com/minimal.example.com/backups/etcd-events
manager:
image: kopeio/etcd-manager:latest
kubernetesVersion: v1.8.0
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

---

apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
creationTimestamp: "2016-12-10T22:42:28Z"
name: nodes
labels:
kops.k8s.io/cluster: minimal.example.com
spec:
associatePublicIp: true
image: kope.io/k8s-1.4-debian-jessie-amd64-hvm-ebs-2016-10-21
machineType: t2.medium
maxSize: 2
minSize: 2
role: Node
subnets:
- us-test-1a

---

apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
creationTimestamp: "2016-12-10T22:42:28Z"
name: master-us-test-1a
labels:
kops.k8s.io/cluster: minimal.example.com
spec:
associatePublicIp: true
image: kope.io/k8s-1.4-debian-jessie-amd64-hvm-ebs-2016-10-21
machineType: m3.medium
maxSize: 1
minSize: 1
role: Master
subnets:
- us-test-1a


Loading

0 comments on commit 666e290

Please sign in to comment.