Skip to content

Commit

Permalink
TEST/MINOR: move test to new test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
hdurand0710 committed Aug 22, 2023
1 parent 5846dd5 commit 6d18884
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 114 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
32 changes: 16 additions & 16 deletions pkg/annotations/cfgSnippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -88,27 +88,27 @@ 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)
}
}
cfgSnippet.disabledSnippets = disable
}

func isConfigSnippetDisabled(name cfgSnippetType) bool {
func IsConfigSnippetDisabled(name CfgSnippetType) bool {
_, disabled := cfgSnippet.disabledSnippets[name]
return disabled
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
97 changes: 0 additions & 97 deletions pkg/annotations/cfgSnippet_test.go

This file was deleted.

113 changes: 113 additions & 0 deletions test/annotations/cfgSnippet_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}
}

0 comments on commit 6d18884

Please sign in to comment.