github.com/joelanford/operator-sdk@v0.8.2/internal/pkg/scaffold/test_setup.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  	"bytes"
    19  	"io"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
    25  
    26  	log "github.com/sirupsen/logrus"
    27  )
    28  
    29  const (
    30  	// test constants describing an app operator project
    31  	appProjectName = "app-operator"
    32  	appRepo        = "github.com" + filePathSep + "example-inc" + filePathSep + appProjectName
    33  	appApiVersion  = "app.example.com/v1alpha1"
    34  	appKind        = "AppService"
    35  )
    36  
    37  var (
    38  	appConfig = &input.Config{
    39  		Repo:           appRepo,
    40  		AbsProjectPath: mustGetImportPath(),
    41  		ProjectName:    appProjectName,
    42  	}
    43  )
    44  
    45  func mustGetImportPath() string {
    46  	wd, err := os.Getwd()
    47  	if err != nil {
    48  		log.Fatalf("Failed to get working directory: (%v)", err)
    49  	}
    50  	return filepath.Join(wd, appRepo)
    51  }
    52  
    53  func setupScaffoldAndWriter() (*Scaffold, *bytes.Buffer) {
    54  	buf := &bytes.Buffer{}
    55  	return &Scaffold{
    56  		GetWriter: func(_ string, _ os.FileMode) (io.Writer, error) {
    57  			return buf, nil
    58  		},
    59  	}, buf
    60  }
    61  
    62  func setupTestFrameworkConfig() (*input.Config, error) {
    63  	absPath, err := os.Getwd()
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	absPath = absPath[:strings.Index(absPath, "internal/pkg")]
    68  	tfDir := filepath.Join(absPath, "test", "test-framework")
    69  
    70  	// Set the project and repo paths to {abs}/test/test-framework, which
    71  	// contains pkg/apis for the memcached-operator.
    72  	return &input.Config{
    73  		Repo:           tfDir[strings.Index(absPath, "github.com"):],
    74  		AbsProjectPath: tfDir,
    75  		ProjectName:    filepath.Base(tfDir),
    76  	}, nil
    77  }