|
| 1 | +/* |
| 2 | + Copyright © 2025 Dell Inc. or its subsidiaries. All Rights Reserved. |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package test |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + // The name of the repctl binary. |
| 31 | + repctlCLI = "repctl" |
| 32 | + // The repctl "create" command used to create k8s resources. |
| 33 | + cmdCreate = "create" |
| 34 | + // Name of the resource provided to the repctl "create" command for creating |
| 35 | + // a storage class. |
| 36 | + resourceStorageClass = "sc" |
| 37 | + // Option used when creating StorageClasses with repctl that allows |
| 38 | + // user to supply a custom repctl config file for generating replication-enabled |
| 39 | + // storage classes. |
| 40 | + optFromConfig = "--from-config" |
| 41 | + // Option provided to repctl to generate output but skip applying. |
| 42 | + optDryRun = "--dry-run" |
| 43 | + |
| 44 | + // name of the environment variable used to provide a path to repctl executable. |
| 45 | + envRepctlPath = "REPCTL_PATH" |
| 46 | +) |
| 47 | + |
| 48 | +// relative location of the directory containing the config files fed to `repctl create sc` command. |
| 49 | +var configFileDir = "testdata" |
| 50 | + |
| 51 | +// getRepctlPath searches for a repctl executable and returns a file path to the executable. |
| 52 | +// If the executable cannot be found, an error is returned. |
| 53 | +// Search paths include csm-replication/repctl, the path provided by environment variable REPCTL_PATH, |
| 54 | +// and PATH, in respective order. |
| 55 | +func getRepctlPath() (path string, err error) { |
| 56 | + fmt.Println("looking for the repctl executable in csm-replication/repctl") |
| 57 | + |
| 58 | + // Try to locate repctl in the default build output location |
| 59 | + _, err = os.Stat("../../repctl") |
| 60 | + if err == nil { |
| 61 | + return "../../repctl", nil |
| 62 | + } |
| 63 | + fmt.Printf("error looking for repctl in csm-replication/repctl: %s\n", err.Error()) |
| 64 | + |
| 65 | + // Try to locate repctl using the path provided via REPCTL_PATH |
| 66 | + fmt.Println("looking for repctl using the path provided via REPCTL_PATH") |
| 67 | + path = filepath.Join(os.Getenv(envRepctlPath), repctlCLI) |
| 68 | + _, err = os.Stat(path) |
| 69 | + if err == nil { |
| 70 | + return path, nil |
| 71 | + } |
| 72 | + fmt.Printf("error looking for repctl in REPCTL_PATH: %s\n", err.Error()) |
| 73 | + |
| 74 | + // Try to locate repctl as part of one of the paths in PATH |
| 75 | + fmt.Println("looking for repctl in PATH") |
| 76 | + path, err = exec.LookPath(repctlCLI) |
| 77 | + if err == nil { |
| 78 | + return path, nil |
| 79 | + } |
| 80 | + fmt.Printf("error looking for repctl in PATH: %s\n", err.Error()) |
| 81 | + |
| 82 | + return "", fmt.Errorf("unable to locate repctl. All methods were exhausted") |
| 83 | +} |
| 84 | + |
| 85 | +// use repctl to generate power scale storage class and compare |
| 86 | +// the output to the expected output |
| 87 | +func TestGeneratePowerScaleStorageClass(t *testing.T) { |
| 88 | + repctl, err := getRepctlPath() |
| 89 | + if err != nil { |
| 90 | + t.Errorf("encountered error while searching for repctl: %s", err.Error()) |
| 91 | + t.SkipNow() |
| 92 | + } |
| 93 | + |
| 94 | + type args struct { |
| 95 | + templateName string |
| 96 | + } |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + args args |
| 100 | + want string |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "generate a csi-powerscale replication storage class with all options populated", |
| 104 | + args: args{ |
| 105 | + templateName: "powerscale", |
| 106 | + }, |
| 107 | + want: powerscaleStorageClass, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + configFilePath := filepath.Join(configFileDir, tt.args.templateName+"_tmpl_values.yaml") |
| 113 | + generated, err := exec.CommandContext(context.Background(), repctl, cmdCreate, resourceStorageClass, optFromConfig, configFilePath, optDryRun).CombinedOutput() |
| 114 | + if err != nil { |
| 115 | + t.Errorf("encountered error while generating storage class: %s", err.Error()) |
| 116 | + t.FailNow() |
| 117 | + } |
| 118 | + |
| 119 | + // remove the log message printed by repctl at the beginning of the print out |
| 120 | + newlineIndex := bytes.IndexRune(generated, '\n') |
| 121 | + generated = generated[newlineIndex:] |
| 122 | + |
| 123 | + assert.Equal(t, string(tt.want), string(generated)) |
| 124 | + } |
| 125 | +} |
0 commit comments