github.com/erdrix/operator-sdk@v0.8.2/pkg/k8sutil/k8sutil_test.go (about)

     1  // Copyright 2018 The Operator-SDK 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  
    15  package k8sutil
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestGetOperatorName(t *testing.T) {
    25  	type Output struct {
    26  		operatorName string
    27  		err          error
    28  	}
    29  
    30  	type Scenario struct {
    31  		name           string
    32  		envVarKey      string
    33  		envVarValue    string
    34  		expectedOutput Output
    35  	}
    36  
    37  	tests := []Scenario{
    38  		{
    39  			name:        "Simple case",
    40  			envVarKey:   OperatorNameEnvVar,
    41  			envVarValue: "myoperator",
    42  			expectedOutput: Output{
    43  				operatorName: "myoperator",
    44  				err:          nil,
    45  			},
    46  		},
    47  		{
    48  			name:        "Unset env var",
    49  			envVarKey:   "",
    50  			envVarValue: "",
    51  			expectedOutput: Output{
    52  				operatorName: "",
    53  				err:          fmt.Errorf("%s must be set", OperatorNameEnvVar),
    54  			},
    55  		},
    56  		{
    57  			name:        "Empty env var",
    58  			envVarKey:   OperatorNameEnvVar,
    59  			envVarValue: "",
    60  			expectedOutput: Output{
    61  				operatorName: "",
    62  				err:          fmt.Errorf("%s must not be empty", OperatorNameEnvVar),
    63  			},
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		_ = os.Setenv(test.envVarKey, test.envVarValue)
    69  		operatorName, err := GetOperatorName()
    70  		if !(operatorName == test.expectedOutput.operatorName && reflect.DeepEqual(err, test.expectedOutput.err)) {
    71  			t.Errorf("Test %s failed, expected output: %s,%v; got: %s,%v", test.name, test.expectedOutput.operatorName, test.expectedOutput.err, operatorName, err)
    72  		}
    73  		_ = os.Unsetenv(test.envVarKey)
    74  	}
    75  }