From d5d0a3f03ae19e2aac4361bd1f1cb41c7d20dbda Mon Sep 17 00:00:00 2001 From: Tomas Kral Date: Fri, 9 Dec 2016 17:00:15 +0100 Subject: [PATCH] Add tests for unsupported keys in loaders --- pkg/loader/bundle/bundle_test.go | 87 ++++++++++++++++++++++++++++++ pkg/loader/compose/compose_test.go | 86 +++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 pkg/loader/bundle/bundle_test.go diff --git a/pkg/loader/bundle/bundle_test.go b/pkg/loader/bundle/bundle_test.go new file mode 100644 index 00000000..d757d459 --- /dev/null +++ b/pkg/loader/bundle/bundle_test.go @@ -0,0 +1,87 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +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 bundle + +import ( + "reflect" + "strings" + "testing" +) + +// TestUnsupportedKeys test checkUnsupportedKey function with various +// docker-compose projects +func TestUnsupportedKeys(t *testing.T) { + user := "user" + workDir := "workDir" + + fullBundle := Bundlefile{ + Version: "0.1", + Services: map[string]Service{ + "foo": Service{ + Image: "image", + Command: []string{"cmd"}, + Args: []string{"arg"}, + Env: []string{"env"}, + Labels: map[string]string{"key": "value"}, + Ports: []Port{Port{Protocol: "tcp", Port: uint32(80)}}, + WorkingDir: &workDir, //there is no other way to get pointer to string + User: &user, + Networks: []string{"net"}, + }, + }, + } + + bundleWithEmptyNetworks := Bundlefile{ + Version: "0.1", + Services: map[string]Service{ + "foo": Service{ + Image: "image", + Command: []string{"cmd"}, + Args: []string{"arg"}, + Env: []string{"env"}, + Labels: map[string]string{"key": "value"}, + Ports: []Port{Port{Protocol: "tcp", Port: uint32(80)}}, + WorkingDir: &workDir, //there is no other way to get pointer to string + User: &user, + Networks: []string{}, + }, + }, + } + // define all test cases for checkUnsupportedKey function + testCases := map[string]struct { + bundleFile Bundlefile + expectedUnsupportedKeys []string + }{ + "Full Bundle": { + fullBundle, + []string{"Networks"}, + }, + "Bundle with empty Networks": { + bundleWithEmptyNetworks, + []string(nil), + }, + } + + for name, test := range testCases { + t.Log("Test case:", name) + keys := checkUnsupportedKey(&test.bundleFile) + if !reflect.DeepEqual(keys, test.expectedUnsupportedKeys) { + t.Errorf("ERROR: Expecting unsupported keys: ['%s']. Got: ['%s']", strings.Join(test.expectedUnsupportedKeys, "', '"), strings.Join(keys, "', '")) + } + } + +} diff --git a/pkg/loader/compose/compose_test.go b/pkg/loader/compose/compose_test.go index 91e7131d..30e5d0e4 100644 --- a/pkg/loader/compose/compose_test.go +++ b/pkg/loader/compose/compose_test.go @@ -18,9 +18,15 @@ package compose import ( "os" + "reflect" + "strings" "testing" "github.com/kubernetes-incubator/kompose/pkg/kobject" + + "github.com/docker/libcompose/config" + "github.com/docker/libcompose/project" + "github.com/docker/libcompose/yaml" ) // Test if service types are parsed properly on user input @@ -112,3 +118,83 @@ func TestLoadEnvVar(t *testing.T) { } } } + +// TestUnsupportedKeys test checkUnsupportedKey function with various +// docker-compose projects +func TestUnsupportedKeys(t *testing.T) { + // create project that will be used in test cases + projectWithNetworks := project.NewProject(&project.Context{}, nil, nil) + projectWithNetworks.ServiceConfigs = config.NewServiceConfigs() + projectWithNetworks.ServiceConfigs.Add("foo", &config.ServiceConfig{ + Image: "foo/bar", + Build: yaml.Build{ + Context: "./build", + }, + Hostname: "localhost", + Ports: []string{}, // test empty array + Networks: &yaml.Networks{ + Networks: []*yaml.Network{ + &yaml.Network{ + Name: "net1", + }, + }, + }, + }) + projectWithNetworks.VolumeConfigs = map[string]*config.VolumeConfig{ + "foo": &config.VolumeConfig{ + Driver: "storage", + }, + } + projectWithNetworks.NetworkConfigs = map[string]*config.NetworkConfig{ + "foo": &config.NetworkConfig{ + Driver: "bridge", + }, + } + + projectWithEmptyNetwork := project.NewProject(&project.Context{}, nil, nil) + projectWithEmptyNetwork.ServiceConfigs = config.NewServiceConfigs() + projectWithEmptyNetwork.ServiceConfigs.Add("foo", &config.ServiceConfig{ + Networks: &yaml.Networks{}, + }) + + projectWithDefaultNetwork := project.NewProject(&project.Context{}, nil, nil) + projectWithDefaultNetwork.ServiceConfigs = config.NewServiceConfigs() + + projectWithDefaultNetwork.ServiceConfigs.Add("foo", &config.ServiceConfig{ + Networks: &yaml.Networks{ + Networks: []*yaml.Network{ + &yaml.Network{ + Name: "default", + }, + }, + }, + }) + + // define all test cases for checkUnsupportedKey function + testCases := map[string]struct { + composeProject *project.Project + expectedUnsupportedKeys []string + }{ + "With Networks (service and root level)": { + projectWithNetworks, + []string{"root level networks", "root level volumes", "hostname", "networks"}, + }, + "Empty Networks on Service level": { + projectWithEmptyNetwork, + []string{"networks"}, + }, + "Default root level Network": { + projectWithDefaultNetwork, + []string(nil), + }, + } + + for name, test := range testCases { + t.Log("Test case:", name) + keys := checkUnsupportedKey(test.composeProject) + if !reflect.DeepEqual(keys, test.expectedUnsupportedKeys) { + t.Errorf("ERROR: Expecting unsupported keys: ['%s']. Got: ['%s']", strings.Join(test.expectedUnsupportedKeys, "', '"), strings.Join(keys, "', '")) + } + } + +}