github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/operator_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 scaffold
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/operator-framework/operator-sdk/internal/util/diffutil"
    21  )
    22  
    23  func TestOperator(t *testing.T) {
    24  	s, buf := setupScaffoldAndWriter()
    25  	err := s.Execute(appConfig, &Operator{})
    26  	if err != nil {
    27  		t.Fatalf("Failed to execute the scaffold: (%v)", err)
    28  	}
    29  
    30  	if operatorExp != buf.String() {
    31  		diffs := diffutil.Diff(operatorExp, buf.String())
    32  		t.Fatalf("Expected vs actual differs.\n%v", diffs)
    33  	}
    34  }
    35  
    36  func TestOperatorClusterScoped(t *testing.T) {
    37  	s, buf := setupScaffoldAndWriter()
    38  	err := s.Execute(appConfig, &Operator{IsClusterScoped: true})
    39  	if err != nil {
    40  		t.Fatalf("Failed to execute the scaffold: (%v)", err)
    41  	}
    42  
    43  	if operatorClusterScopedExp != buf.String() {
    44  		diffs := diffutil.Diff(operatorClusterScopedExp, buf.String())
    45  		t.Fatalf("Expected vs actual differs.\n%v", diffs)
    46  	}
    47  }
    48  
    49  const operatorExp = `apiVersion: apps/v1
    50  kind: Deployment
    51  metadata:
    52    name: app-operator
    53  spec:
    54    replicas: 1
    55    selector:
    56      matchLabels:
    57        name: app-operator
    58    template:
    59      metadata:
    60        labels:
    61          name: app-operator
    62      spec:
    63        serviceAccountName: app-operator
    64        containers:
    65          - name: app-operator
    66            # Replace this with the built image name
    67            image: REPLACE_IMAGE
    68            command:
    69            - app-operator
    70            imagePullPolicy: Always
    71            env:
    72              - name: WATCH_NAMESPACE
    73                valueFrom:
    74                  fieldRef:
    75                    fieldPath: metadata.namespace
    76              - name: POD_NAME
    77                valueFrom:
    78                  fieldRef:
    79                    fieldPath: metadata.name
    80              - name: OPERATOR_NAME
    81                value: "app-operator"
    82  `
    83  
    84  const operatorClusterScopedExp = `apiVersion: apps/v1
    85  kind: Deployment
    86  metadata:
    87    name: app-operator
    88  spec:
    89    replicas: 1
    90    selector:
    91      matchLabels:
    92        name: app-operator
    93    template:
    94      metadata:
    95        labels:
    96          name: app-operator
    97      spec:
    98        serviceAccountName: app-operator
    99        containers:
   100          - name: app-operator
   101            # Replace this with the built image name
   102            image: REPLACE_IMAGE
   103            command:
   104            - app-operator
   105            imagePullPolicy: Always
   106            env:
   107              - name: WATCH_NAMESPACE
   108                value: ""
   109              - name: POD_NAME
   110                valueFrom:
   111                  fieldRef:
   112                    fieldPath: metadata.name
   113              - name: OPERATOR_NAME
   114                value: "app-operator"
   115  `