From 6d188841f953e87a097e51c60542ae0b7e7112a2 Mon Sep 17 00:00:00 2001 From: Helene Durand Date: Tue, 22 Aug 2023 11:22:01 +0200 Subject: [PATCH] TEST/MINOR: move test to new test folder --- main.go | 3 +- pkg/annotations/cfgSnippet.go | 32 ++++---- pkg/annotations/cfgSnippet_test.go | 97 ------------------------ test/annotations/cfgSnippet_test.go | 113 ++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 114 deletions(-) delete mode 100644 pkg/annotations/cfgSnippet_test.go create mode 100644 test/annotations/cfgSnippet_test.go diff --git a/main.go b/main.go index bc384b88..2dddaf51 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,8 @@ func main() { } logger.ShowFilename(true) + annotations.DisableConfigSnippets(osArgs.DisableConfigSnippets) + // backwards compatibility with 1.7 if osArgs.PprofEnabled && osArgs.ControllerPort == 0 { osArgs.ControllerPort = 6060 @@ -194,7 +196,6 @@ func logInfo(logger utils.Logger, osArgs utils.OSArgs) bool { } if osArgs.DisableConfigSnippets != "" { logger.Printf("Disabling config snippets for [%s]", osArgs.DisableConfigSnippets) - annotations.DisableConfigSnippets(osArgs.DisableConfigSnippets) } logger.Debugf("Kubernetes Informers resync period: %s", osArgs.CacheResyncPeriod.String()) logger.Printf("Controller sync period: %s\n", osArgs.SyncPeriod.String()) diff --git a/pkg/annotations/cfgSnippet.go b/pkg/annotations/cfgSnippet.go index f133d9b7..4370d1fb 100644 --- a/pkg/annotations/cfgSnippet.go +++ b/pkg/annotations/cfgSnippet.go @@ -43,13 +43,13 @@ type cfgData struct { } // CfgSnippetType represents type of a config snippet -type cfgSnippetType string +type CfgSnippetType string const ( // CfgSnippetType values - configSnippetBackend cfgSnippetType = "backend" - configSnippetFrontend cfgSnippetType = "frontend" - configSnippetGlobal cfgSnippetType = "global" + ConfigSnippetBackend CfgSnippetType = "backend" + ConfigSnippetFrontend CfgSnippetType = "frontend" + ConfigSnippetGlobal CfgSnippetType = "global" ) // cfgSnippet is a particular type of config that is not @@ -66,7 +66,7 @@ var cfgSnippet struct { backends map[string]map[string]*cfgData // backends[backend][origin] = &cfgData{} disabledServices map[string]bool // Flags to allow disable some config snippet ("backend", "frontend", "global") - disabledSnippets map[cfgSnippetType]struct{} + disabledSnippets map[CfgSnippetType]struct{} } func init() { //nolint:gochecknoinits @@ -88,19 +88,19 @@ type ConfigSnippetOptions struct { // from a comma separated list : all,backend,frontend,global. // If "all" is present in the list, then: backend, frontend and global config snippets are disabled. func DisableConfigSnippets(disableConfigSnippets string) { - disable := map[cfgSnippetType]struct{}{} + disable := map[CfgSnippetType]struct{}{} for _, d := range strings.Split(disableConfigSnippets, ",") { switch strings.TrimSpace(d) { case "all": - disable[configSnippetBackend] = struct{}{} - disable[configSnippetFrontend] = struct{}{} - disable[configSnippetGlobal] = struct{}{} + disable[ConfigSnippetBackend] = struct{}{} + disable[ConfigSnippetFrontend] = struct{}{} + disable[ConfigSnippetGlobal] = struct{}{} case "frontend": - disable[configSnippetFrontend] = struct{}{} + disable[ConfigSnippetFrontend] = struct{}{} case "backend": - disable[configSnippetBackend] = struct{}{} + disable[ConfigSnippetBackend] = struct{}{} case "global": - disable[configSnippetGlobal] = struct{}{} + disable[ConfigSnippetGlobal] = struct{}{} default: logger.Errorf("wrong config snippet type '%s' in disable-config-snippets arg in command line", d) } @@ -108,7 +108,7 @@ func DisableConfigSnippets(disableConfigSnippets string) { cfgSnippet.disabledSnippets = disable } -func isConfigSnippetDisabled(name cfgSnippetType) bool { +func IsConfigSnippetDisabled(name CfgSnippetType) bool { _, disabled := cfgSnippet.disabledSnippets[name] return disabled } @@ -137,7 +137,7 @@ func (a *CfgSnippet) GetName() string { func (a *CfgSnippet) Process(k store.K8s, annotations ...map[string]string) error { switch { case a.frontend != "": - if isConfigSnippetDisabled(configSnippetFrontend) { + if IsConfigSnippetDisabled(ConfigSnippetFrontend) { // frontend snippet is disabled, do not handle return nil } @@ -158,7 +158,7 @@ func (a *CfgSnippet) Process(k store.K8s, annotations ...map[string]string) erro } case a.backend != "": - if isConfigSnippetDisabled(configSnippetBackend) { + if IsConfigSnippetDisabled(ConfigSnippetBackend) { // backend snippet is disabled, do not handle return nil } @@ -196,7 +196,7 @@ func (a *CfgSnippet) Process(k store.K8s, annotations ...map[string]string) erro } } default: - if isConfigSnippetDisabled(configSnippetGlobal) { + if IsConfigSnippetDisabled(ConfigSnippetGlobal) { // global snippet is disabled, do not handle return nil } diff --git a/pkg/annotations/cfgSnippet_test.go b/pkg/annotations/cfgSnippet_test.go deleted file mode 100644 index 2f58b131..00000000 --- a/pkg/annotations/cfgSnippet_test.go +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2023 HAProxy Technologies LLC -// -// 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 annotations - -import ( - "reflect" - "testing" -) - -func Test_DisableConfigSnippets(t *testing.T) { - tests := []struct { - name string - disableConfigSnippets string - want map[cfgSnippetType]struct{} - }{ - { - name: "No meaningful value", - disableConfigSnippets: "invalid", - want: map[cfgSnippetType]struct{}{}, - }, - { - name: "all", - disableConfigSnippets: "all", - want: map[cfgSnippetType]struct{}{ - configSnippetBackend: {}, - configSnippetFrontend: {}, - configSnippetGlobal: {}, - }, - }, - { - name: "frontend only", - disableConfigSnippets: "frontend", - want: map[cfgSnippetType]struct{}{ - configSnippetFrontend: {}, - }, - }, - { - name: "backend only", - disableConfigSnippets: "backend", - want: map[cfgSnippetType]struct{}{ - configSnippetBackend: {}, - }, - }, - { - name: "global only", - disableConfigSnippets: "global", - want: map[cfgSnippetType]struct{}{ - configSnippetGlobal: {}, - }, - }, - { - name: "frontend and backend", - disableConfigSnippets: "backend,frontend", - want: map[cfgSnippetType]struct{}{ - configSnippetFrontend: {}, - configSnippetBackend: {}, - }, - }, - { - name: "frontend and global, whitespaces", - disableConfigSnippets: " frontend, global", - want: map[cfgSnippetType]struct{}{ - configSnippetGlobal: {}, - configSnippetFrontend: {}, - }, - }, - { - name: "frontend global, backend", - disableConfigSnippets: "backend,global,frontend", - want: map[cfgSnippetType]struct{}{ - configSnippetBackend: {}, - configSnippetFrontend: {}, - configSnippetGlobal: {}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - DisableConfigSnippets(tt.disableConfigSnippets) - if !reflect.DeepEqual(cfgSnippet.disabledSnippets, tt.want) { - t.Errorf("DisabledConfigSnippets() = %v, want %v", cfgSnippet.disabledSnippets, tt.want) - } - }) - } -} diff --git a/test/annotations/cfgSnippet_test.go b/test/annotations/cfgSnippet_test.go new file mode 100644 index 00000000..a84b6235 --- /dev/null +++ b/test/annotations/cfgSnippet_test.go @@ -0,0 +1,113 @@ +// Copyright 2023 HAProxy Technologies LLC +// +// 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 annotations_test + +import ( + "testing" + + "github.com/haproxytech/kubernetes-ingress/pkg/annotations" +) + +func Test_DisableConfigSnippets(t *testing.T) { + tests := []struct { + name string + disableConfigSnippets string + want map[annotations.CfgSnippetType]bool + }{ + { + name: "No meaningful value", + disableConfigSnippets: "invalid", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetBackend: false, + annotations.ConfigSnippetFrontend: false, + annotations.ConfigSnippetGlobal: false, + }, + }, + { + name: "all", + disableConfigSnippets: "all", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetBackend: true, + annotations.ConfigSnippetFrontend: true, + annotations.ConfigSnippetGlobal: true, + }, + }, + { + name: "frontend only", + disableConfigSnippets: "frontend", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetFrontend: true, + annotations.ConfigSnippetBackend: false, + annotations.ConfigSnippetGlobal: false, + }, + }, + { + name: "backend only", + disableConfigSnippets: "backend", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetBackend: true, + annotations.ConfigSnippetFrontend: false, + annotations.ConfigSnippetGlobal: false, + }, + }, + { + name: "global only", + disableConfigSnippets: "global", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetGlobal: true, + annotations.ConfigSnippetFrontend: false, + annotations.ConfigSnippetBackend: false, + }, + }, + { + name: "frontend and backend", + disableConfigSnippets: "backend,frontend", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetFrontend: true, + annotations.ConfigSnippetBackend: true, + annotations.ConfigSnippetGlobal: false, + }, + }, + { + name: "frontend and global, whitespaces", + disableConfigSnippets: " frontend, global", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetGlobal: true, + annotations.ConfigSnippetFrontend: true, + annotations.ConfigSnippetBackend: false, + }, + }, + { + name: "frontend global, backend", + disableConfigSnippets: "backend,global,frontend", + want: map[annotations.CfgSnippetType]bool{ + annotations.ConfigSnippetBackend: true, + annotations.ConfigSnippetFrontend: true, + annotations.ConfigSnippetGlobal: true, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + annotations.DisableConfigSnippets(tt.disableConfigSnippets) + for snippetType, wantDisabled := range tt.want { + disabled := annotations.IsConfigSnippetDisabled(snippetType) + if disabled != wantDisabled { + t.Errorf("DisabledConfigSnippets() for type %s = %v, want %v", snippetType, disabled, wantDisabled) + } + } + }) + } +}