|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAnalyzeCustomResourceDefinition(t *testing.T) { |
| 14 | + getFile := func(_ string) ([]byte, error) { |
| 15 | + crdsList := v1beta1.CustomResourceDefinitionList{ |
| 16 | + Items: []v1beta1.CustomResourceDefinition{ |
| 17 | + {ObjectMeta: metav1.ObjectMeta{Name: "servicemonitors.monitoring.coreos.com"}}, |
| 18 | + {ObjectMeta: metav1.ObjectMeta{Name: "probes.monitoring.coreos.com"}}, |
| 19 | + }, |
| 20 | + } |
| 21 | + return json.Marshal(crdsList) |
| 22 | + } |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + analyzer *troubleshootv1beta2.CustomResourceDefinition |
| 27 | + expectedResult *AnalyzeResult |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "CRD exists and pass", |
| 31 | + analyzer: &troubleshootv1beta2.CustomResourceDefinition{ |
| 32 | + CustomResourceDefinitionName: "servicemonitors.monitoring.coreos.com", |
| 33 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 34 | + { |
| 35 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 36 | + Message: "The ServiceMonitor CRD is installed and available.", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + expectedResult: &AnalyzeResult{ |
| 42 | + Title: "Custom resource definition servicemonitors.monitoring.coreos.com", |
| 43 | + IconKey: "kubernetes_custom_resource_definition", |
| 44 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/custom-resource-definition.svg?w=13&h=16", |
| 45 | + IsPass: true, |
| 46 | + Message: "The ServiceMonitor CRD is installed and available.", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "CRD not exists and warn", |
| 51 | + analyzer: &troubleshootv1beta2.CustomResourceDefinition{ |
| 52 | + CustomResourceDefinitionName: "podmonitors.monitoring.coreos.com", |
| 53 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 54 | + { |
| 55 | + Warn: &troubleshootv1beta2.SingleOutcome{ |
| 56 | + Message: "The Prometheus PodMonitor CRD was not found in the cluster.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + expectedResult: &AnalyzeResult{ |
| 62 | + Title: "Custom resource definition podmonitors.monitoring.coreos.com", |
| 63 | + IconKey: "kubernetes_custom_resource_definition", |
| 64 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/custom-resource-definition.svg?w=13&h=16", |
| 65 | + IsWarn: true, |
| 66 | + Message: "The Prometheus PodMonitor CRD was not found in the cluster.", |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "CRD not exists and fail", |
| 71 | + analyzer: &troubleshootv1beta2.CustomResourceDefinition{ |
| 72 | + CustomResourceDefinitionName: "backupstoragelocations.velero.io", |
| 73 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 74 | + { |
| 75 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 76 | + Message: "The BackupStorageLocation CRD was not found in the cluster.", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + expectedResult: &AnalyzeResult{ |
| 82 | + Title: "Custom resource definition backupstoragelocations.velero.io", |
| 83 | + IconKey: "kubernetes_custom_resource_definition", |
| 84 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/custom-resource-definition.svg?w=13&h=16", |
| 85 | + IsFail: true, |
| 86 | + Message: "The BackupStorageLocation CRD was not found in the cluster.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + a := &AnalyzeCustomResourceDefinition{ |
| 94 | + analyzer: tt.analyzer, |
| 95 | + } |
| 96 | + result, err := a.analyzeCustomResourceDefinition(tt.analyzer, getFile) |
| 97 | + assert.NoError(t, err) |
| 98 | + assert.Equal(t, tt.expectedResult, result) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments