|
| 1 | +// Copyright 2021 OnMetal authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package switches |
| 15 | + |
| 16 | +import ( |
| 17 | + "flag" |
| 18 | + |
| 19 | + . "github.com/onsi/ginkgo" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | + "github.com/spf13/pflag" |
| 22 | + "sigs.k8s.io/kustomize/kyaml/sets" |
| 23 | +) |
| 24 | + |
| 25 | +var _ = Describe("CMD Switches", func() { |
| 26 | + Context("Testing Switches interface", func() { |
| 27 | + It("should disable runner", func() { |
| 28 | + s := New([]string{"runner-a", Disable("runner-b")}) |
| 29 | + Expect(s.Enabled("runner-a")).To(BeTrue()) |
| 30 | + Expect(s.Enabled("runner-b")).To(BeFalse()) |
| 31 | + }) |
| 32 | + It("should return all items", func() { |
| 33 | + s := New([]string{"runner-a", Disable("runner-b")}) |
| 34 | + |
| 35 | + expected := make(sets.String) |
| 36 | + expected.Insert("runner-a", "runner-b") |
| 37 | + Expect(s.All()).To(Equal(expected)) |
| 38 | + }) |
| 39 | + It("should return all disabled items", func() { |
| 40 | + s := New([]string{"runner-a", Disable("runner-b")}) |
| 41 | + |
| 42 | + expected := make(sets.String) |
| 43 | + expected.Insert("runner-b") |
| 44 | + Expect(s.DisabledByDefault()).To(Equal(expected)) |
| 45 | + }) |
| 46 | + It("should return string", func() { |
| 47 | + s := New([]string{"runner-a", Disable("runner-b")}) |
| 48 | + |
| 49 | + Expect(s.String()).To(Equal("map[runner-a:true runner-b:false]")) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + Context("Testing flag package behavior", func() { |
| 54 | + It("should keep default settings when no flag is passed", func() { |
| 55 | + fs := flag.NewFlagSet("", flag.ExitOnError) |
| 56 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 57 | + fs.Var(controllers, "controllers", "") |
| 58 | + |
| 59 | + Expect(fs.Parse([]string{})).NotTo(HaveOccurred()) |
| 60 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 61 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 62 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 63 | + }) |
| 64 | + It("should keep default settings when * is passed", func() { |
| 65 | + fs := flag.NewFlagSet("", flag.ExitOnError) |
| 66 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 67 | + fs.Var(controllers, "controllers", "") |
| 68 | + |
| 69 | + Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred()) |
| 70 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 71 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 72 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 73 | + }) |
| 74 | + It("should override default settings", func() { |
| 75 | + fs := flag.NewFlagSet("", flag.ExitOnError) |
| 76 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 77 | + fs.Var(controllers, "controllers", "") |
| 78 | + |
| 79 | + Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred()) |
| 80 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 81 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 82 | + Expect(controllers.Enabled("runner-c")).To(BeFalse()) |
| 83 | + }) |
| 84 | + It("should override some of default settings", func() { |
| 85 | + fs := flag.NewFlagSet("", flag.ExitOnError) |
| 86 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 87 | + fs.Var(controllers, "controllers", "") |
| 88 | + |
| 89 | + Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred()) |
| 90 | + Expect(controllers.Enabled("runner-a")).To(BeFalse()) |
| 91 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 92 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + Context("Testing pflag package behavior", func() { |
| 97 | + It("should keep default settings when no flag is passed", func() { |
| 98 | + fs := pflag.NewFlagSet("", pflag.ExitOnError) |
| 99 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 100 | + fs.Var(controllers, "controllers", "") |
| 101 | + |
| 102 | + Expect(fs.Parse([]string{})).NotTo(HaveOccurred()) |
| 103 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 104 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 105 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 106 | + }) |
| 107 | + It("should keep default settings when * is passed", func() { |
| 108 | + fs := pflag.NewFlagSet("", pflag.ExitOnError) |
| 109 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 110 | + fs.Var(controllers, "controllers", "") |
| 111 | + |
| 112 | + Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred()) |
| 113 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 114 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 115 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 116 | + }) |
| 117 | + It("should override default settings", func() { |
| 118 | + fs := pflag.NewFlagSet("", pflag.ExitOnError) |
| 119 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 120 | + fs.Var(controllers, "controllers", "") |
| 121 | + |
| 122 | + Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred()) |
| 123 | + Expect(controllers.Enabled("runner-a")).To(BeTrue()) |
| 124 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 125 | + Expect(controllers.Enabled("runner-c")).To(BeFalse()) |
| 126 | + }) |
| 127 | + It("should override some of default settings", func() { |
| 128 | + fs := pflag.NewFlagSet("", pflag.ExitOnError) |
| 129 | + controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) |
| 130 | + fs.Var(controllers, "controllers", "") |
| 131 | + |
| 132 | + Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred()) |
| 133 | + Expect(controllers.Enabled("runner-a")).To(BeFalse()) |
| 134 | + Expect(controllers.Enabled("runner-b")).To(BeFalse()) |
| 135 | + Expect(controllers.Enabled("runner-c")).To(BeTrue()) |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
0 commit comments