...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/chaos-builder/test.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/chaos-builder

     1  // Copyright 2021 Chaos Mesh 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  
    16  package main
    17  
    18  import (
    19  	"bytes"
    20  	"text/template"
    21  )
    22  
    23  const testImport = `
    24  import (
    25  	"reflect"
    26  	"testing"
    27  
    28  	"github.com/bxcodec/faker"
    29  	. "github.com/onsi/gomega"
    30  )
    31  `
    32  
    33  const testInit = `
    34  func init() {
    35  	faker.AddProvider("ioMethods", func(v reflect.Value) (interface{}, error) {
    36  		return []IoMethod{LookUp}, nil
    37  	})
    38  }
    39  `
    40  
    41  const testTemplate = `
    42  func Test{{.Type}}IsDeleted(t *testing.T) {
    43  	g := NewGomegaWithT(t)
    44  
    45  	chaos := &{{.Type}}{}
    46  	err := faker.FakeData(chaos)
    47  
    48  	g.Expect(err).To(BeNil())
    49  
    50  	chaos.IsDeleted()
    51  }
    52  
    53  func Test{{.Type}}IsIsPaused(t *testing.T) {
    54  	g := NewGomegaWithT(t)
    55  
    56  	chaos := &{{.Type}}{}
    57  	err := faker.FakeData(chaos)
    58  
    59  	g.Expect(err).To(BeNil())
    60  
    61  	chaos.IsPaused()
    62  }
    63  
    64  func Test{{.Type}}GetDuration(t *testing.T) {
    65  	g := NewGomegaWithT(t)
    66  
    67  	chaos := &{{.Type}}{}
    68  	err := faker.FakeData(chaos)
    69  
    70  	g.Expect(err).To(BeNil())
    71  
    72  	chaos.Spec.GetDuration()
    73  }
    74  
    75  func Test{{.Type}}GetStatus(t *testing.T) {
    76  	g := NewGomegaWithT(t)
    77  
    78  	chaos := &{{.Type}}{}
    79  	err := faker.FakeData(chaos)
    80  
    81  	g.Expect(err).To(BeNil())
    82  
    83  	chaos.GetStatus()
    84  }
    85  
    86  func Test{{.Type}}GetSpecAndMetaString(t *testing.T) {
    87  	g := NewGomegaWithT(t)
    88  	chaos := &{{.Type}}{}
    89  	err := faker.FakeData(chaos)
    90  	g.Expect(err).To(BeNil())
    91  	chaos.GetSpecAndMetaString()
    92  }
    93  
    94  func Test{{.Type}}ListChaos(t *testing.T) {
    95  	g := NewGomegaWithT(t)
    96  
    97  	chaos := &{{.Type}}List{}
    98  	err := faker.FakeData(chaos)
    99  
   100  	g.Expect(err).To(BeNil())
   101  
   102  	chaos.ListChaos()
   103  }
   104  `
   105  
   106  func generateTest(name string) string {
   107  	tmpl, err := template.New("test").Parse(testTemplate)
   108  	if err != nil {
   109  		log.Error(err, "fail to build template")
   110  		return ""
   111  	}
   112  
   113  	buf := new(bytes.Buffer)
   114  	err = tmpl.Execute(buf, &metadata{
   115  		Type: name,
   116  	})
   117  	if err != nil {
   118  		log.Error(err, "fail to execute template")
   119  		return ""
   120  	}
   121  
   122  	return buf.String()
   123  }
   124