|
| 1 | +/* |
| 2 | +Copyright 2024 The KEDA Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + "k8s.io/apimachinery/pkg/version" |
| 27 | + "k8s.io/client-go/discovery" |
| 28 | + restclient "k8s.io/client-go/rest" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + |
| 31 | + "github.com/kedacore/keda-olm-operator/controllers/keda/util" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("Checking for the OpenShift RuntimeDefault seccomp profile", func() { |
| 35 | + logger := ctrl.Log.WithName("test") |
| 36 | + testData := []struct { |
| 37 | + version version.Info |
| 38 | + hasRuntimeDefault bool |
| 39 | + context string |
| 40 | + }{ |
| 41 | + {version.Info{Major: "1", Minor: "28", GitCommit: "baz"}, false, "When running on a recent cluster"}, |
| 42 | + {version.Info{Major: "1", Minor: "28+", GitCommit: "baz"}, false, "When running on a recent cluster with extra chars in the minor version"}, |
| 43 | + {version.Info{Major: "1", Minor: "21", GitCommit: "baz"}, true, "When running on an old cluster"}, |
| 44 | + {version.Info{Major: "1", Minor: "", GitCommit: "baz"}, false, "When running on a cluster with no minor version"}, |
| 45 | + {version.Info{Major: "1", Minor: "+", GitCommit: "baz"}, false, "When running on a cluster with a garbage minor version"}, |
| 46 | + } |
| 47 | + for _, tt := range testData { |
| 48 | + Context(tt.context, func() { |
| 49 | + It("Should be able to determine whether RuntimeDefault exists by getting the cluster version", func() { |
| 50 | + if testType != "unit" { |
| 51 | + Skip("test.type isn't 'unit'") |
| 52 | + } |
| 53 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 54 | + output, err := json.Marshal(tt.version) |
| 55 | + Expect(err).To(BeNil()) |
| 56 | + |
| 57 | + w.Header().Set("Content-Type", "application/json") |
| 58 | + w.WriteHeader(http.StatusOK) |
| 59 | + _, _ = w.Write(output) |
| 60 | + })) |
| 61 | + defer server.Close() |
| 62 | + |
| 63 | + client := discovery.NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL}) |
| 64 | + got := util.RunningOnClusterWithoutSeccompProfileDefault(logger, client) |
| 65 | + |
| 66 | + Expect(got).To(Equal(tt.hasRuntimeDefault)) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + } |
| 71 | +}) |
0 commit comments