github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/core/generate_resources_test.go (about)

     1  /*
     2   * Copyright 2018 the original author or authors.
     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   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *   Unless required by applicable law or agreed to in writing, software
    11   *   distributed under the License is distributed on an "AS IS" BASIS,
    12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *   See the License for the specific language governing permissions and
    14   *   limitations under the License.
    15   */
    16  
    17  package core
    18  
    19  import (
    20  	"testing"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/projectriff/riff-cli/pkg/options"
    23  	"gopkg.in/yaml.v2"
    24  )
    25  
    26  func TestTopics(t *testing.T) {
    27  	as := assert.New(t)
    28  
    29  	opts := options.InitOptions{
    30  		FunctionName: "myfunc",
    31  		Input:        "in",
    32  		Output:       "out",
    33  	}
    34  	topic, err := createTopics(opts)
    35  
    36  	as.NoError(err)
    37  	as.Contains(topic, "name: in")
    38  	as.Contains(topic, "name: out")
    39  }
    40  
    41  type YFunction struct {
    42  	ApiVersion string
    43  	Kind       string
    44  	Metadata struct {
    45  		Name string
    46  	}
    47  	Spec struct {
    48  		Protocol string
    49  		Input    string
    50  		Container struct {
    51  			Image string
    52  		}
    53  	}
    54  }
    55  
    56  func TestFunction(t *testing.T) {
    57  	as := assert.New(t)
    58  
    59  	opts := options.InitOptions{
    60  		FunctionName: "myfunc",
    61  		Input:        "in",
    62  		Output:       "out",
    63  		Protocol:     "http",
    64  		UserAccount:  "me",
    65  		Version:      "0.0.1",
    66  	}
    67  
    68  	f, err := DefaultGenerateFunction(opts)
    69  	as.NoError(err)
    70  	as.Contains(f, "input:")
    71  	as.Contains(f, "output:")
    72  
    73  	opts.Output = ""
    74  
    75  	f, err = DefaultGenerateFunction(opts)
    76  	as.NoError(err)
    77  
    78  	yf := YFunction{}
    79  	err = yaml.Unmarshal([]byte(f), &yf)
    80  	as.NoError(err)
    81  	as.Equal("http", yf.Spec.Protocol)
    82  	as.Equal("in", yf.Spec.Input)
    83  	as.Equal("myfunc", yf.Metadata.Name)
    84  	as.Equal("me/myfunc:0.0.1", yf.Spec.Container.Image)
    85  }