扫码阅读
手机扫码阅读

云原生|干货来袭!Kubernetes Operator测试实例

482 2023-09-23

本期摘要

Kubebuilder是一种基于Go语言的开发框架,它提供了一些有用的工具和库,可以帮助开发人员快速构建和测试Kubernetes Operator。


本文以kubebuilder框架为例,将主要描述以kubebuilder框架为基础的情况下开发的operator如何进行相关的operator的的测试。

作者

冯康 | Go开发工程师

非正常程序员

01

主要代码介绍

首先在kubebuilder的框架生成时,kubebuilder框架本身会将测试代码的文件生成

进入该文件之后可以看到测试文件使用的测试包为

ginkgo和gomega包为TDD类型的go语言的实现,TDD即test-driven-development测试驱动开发(关于TDD的详情可以自己google,本文不做展开描述)

接下来我们看代码部分,代码部分可以分为4部分

  • 变量定义

  • 测试入口

  • before函数

  • after函数

//变量定义

包含了一个rest.Config的指针,这个指针实际表示的即为测试集群的配置文件即kubeconfig,client.Client为一个测试集群的客户端,envtest.Environment即为测试环境的抽象对象

//测试程序入口

这里注册了测试程序中报错的handler处理函数,以及兼容原生go test 的运行函数

//before函数

在before函数中设置了日志相关的设置、读取CRD文件的、以及启动测试环境,将crd的schema添加到k8s的默认的schema中以及根据这个schema生成对应的client。

然而,这个自动生成的文件缺少了实际启动控制器的方法。上面的代码将会建立一个和您自定义的 Kind 交互的客户端,但是无法测试您的控制器的行为。

如果你想要测试自定义的控制器逻辑,您需要添加一些相似的管理逻辑到 BeforeSuite() 函数, 这样就可以将你的自定义控制器运行在这个测试集群上。

一旦添加了下面的代码,你将可以删除掉上面的 k8sClient,因为你可以从 manager 中获取到 k8sClient (如下所示)。


k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
    Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())
 
err = (&CronJobReconciler{
    Client: k8sManager.GetClient(),
    Log: ctrl.Log.WithName("controllers").WithName("CronJob"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred()) go func() {
    err = k8sManager.Start(ctrl.SetupSignalHandler())
    Expect(err).ToNot(HaveOccurred())
}()
k8sClient = k8sManager.GetClient()
Expect(k8sClient).ToNot(BeNil()) close(done)

熟悉kububuilder框架的程序员应该能发现这里的代码即为生成的默认main函数中和初始化manager部分的代码是相同的

其中可以看到大量的Expect()函数,可以看到下图

该函数主要是接受一个任意类型的变量并将起转换为Assertion类型的接口,其中Assertion类型的接口则包含了以下几种方法:

这些方法即表示传入的变量是否应该为某个值,即以下的代码

Expect(cfg).NotTo(BeNil())

等价于

if cfg != nil{ return err
}

//after函数

该函数主要停止了testEnv。

当然由于这里还是需要使用client到k8s中去创建对应的crd。注意如果你的集群中已经安装了对应的crd的operator的话,实际在集群中创建的cr资源仍然会被集群中的operator list-watch到,可能会导致集群的operator和测试代码中的operator对资源处理进行抢占。

如果不希望发生这样的情况,可以选择在生成client时直接选择使用集群的配置文件去生成对应的client并且不再额外启动manger,或者选择使用一个并未安装operator的集群进行测试。

现在我们已经了解了suite_test文件的主要代码,接下来我们开始根据我们的实际情况来编写测试代码

02

实际测试

一、块划分

在较为复杂的项目中,功能点也会非常的多,相应的我们的测试用例也会非常的多,这时测试用例的编排就显得尤为必要,良好的排布用例可以方便我们在后期的维护中快速的找到对应功能的测试用例,进行更改或者开发。

在gomega中我们则使用使用了以下几类函数来对用例进行划块Describe、Context、When、It、Specify,这里使用的是几类函数而不是说几个函数,是因为这几个函数和fmt包中的print函数相同。

除了基础Print函数外还有Sprint和Fprint函数,而对应到Describe、Context、When、It、Specify这几类中则是P,F,X即Describe,PDescribe,FDescribe,XDescribe等等,而他们的含义则是在基础函数的原本功能上增加了Focus、Pend具体的说明将在下文中说到。

首先是Describe、Context、When这三类函数,他们可以说基本上是等价的,他们的功能就是用来划分你的测试用例,将功能模块相同的用例放在一起,在他们的闭包函数中可以包含块统一处理函数如BeforeEach、AfterEach等这些函数将在第二部分块设置中进行详细的说明。

那么为什么又说这三者基本是等价的呢,我们可以通过源码看到Context类的函数是直接等于Describe函数的,而When函数的实际实现和Describe的实现基本相同只不过会在text的前添加when字符串,可能这样仍然不好理解,我们看以下源码:

Describe:

Context:

When:

可以清晰的看到Context类的函数是直接等于Describe类函数的,所以这两类函数是完全等价的。

而对照来看Describe类函数和When类函数其中唯一的差别则是在When函数会自动在text值前加上When字符串,然而在实际的使用中则没有差别,唯一的差别就是测试报告的显示上。

那么现在明确了Describe、Context、When这三类函数的功能则是为了划分块将功能模块相同的测试用例划分到统一个块中。

但是如果仅仅是为了将功能模块相同的用例放在一起那么实际对于开发者来说仍然可以通过良好的编码习惯来进行控制,那么这些函数产生的实际意义其实除了块划分以外,另一个比较重要的点则是对同一个块中的用例进行统一处理这部分的说明这将在第二部分块设置中说明。

那么接下来就是另外两类函数It、Specify,相应的其中It和Specify也是完全等价的,话不多说直接上源码

我们可以看到Specify类的函数是完全等价于It类函数的,并且我们发现It函数和Describe函数实现也是非常的像,唯一不同的地方就是NodeType,//todo。

在实际的使用中我们往往会使用块函数组织一个较多较为复杂的测试用例,但是在某次的代码改动中如果我们只更改了某个模块或者功能的代码,实际上我们并不用执行全部的测试用例,而是只执行这一部分功能的测试用例即可。

但是如果是注释其他测试代码或者删除其他测试代码,这非常不优雅。

而P,F,X的函数则是提供了一种快速标记的方法,比如我们只需要执行某个Describe块中的用例甚至某个It块中的用例,那么我们则可以将该块的Describe或者It函数改为FDescribe或者FIt,则在执行是gomage则会只执行对应块中的用例,或者我们只有某个功能模块没有更改时,那么这个模块的用例则不用再次执行,我们只需要将Describe或者It该为PDescribe或者PIt那么gomega在执行时则会忽略这个块中的用例,但是由于我们的块中存在嵌套,如果内层和外层同时设置focus时则外层的focus会失效只执行内存的。

但是在功能较为相似的的块划分中如果我们可以更抽象的表示一些用例则可以更加简化我们的测试代码,这时我们则可以选择使用DescribeTable类函数,同样的它也拥有P、F、X的函数,不同的是我们可以将功能类似的函数简化如下边的一个官方的例子

DescribeTable("a simple table", func(x int, y int, expected bool) {
        Ω(x > y).Should(Equal(expected))
    },
    Entry("x > y", 1, 0, true),
    Entry("x == y", 0, 0, false),
    Entry("x < y", 0, 1, false),
)

当然对于一个较为复杂或者运行时常较为长的用例中我们希望可以观察到用例执行到具体的位置,在传统的写法中我们则会进行日志的打印,而在gomega中我们则使用By函数,它除了进行简单的标识打印,同时它还可以接受更多的函数,并在执行到By函数时立即执行传入的函数。

二、块设置

在第一部分中我们通过块函数将较为复杂的测试用例根据模块功能等进行了具体的划分。

但是在相同的功能的模块的测试用例中我们往会想在用例代码执行前或后做些前置操作或后置操作,如生成测试数据,删除测试数据等,而这些代码往往是重复的独立于测试逻辑之外的,如果写在每个It块的函数中则需要写大量的重复的代码,这时我们则希望可以有一个函数可以统一的在It 的前生成测试数据或在It执行后删除测试数据。

这时我们则可以选择使用块设置函数如全局的测试用例的设置BeforeSuite、AfterSuite,或者在具体的块中进行BeforeEach、AfterEach、JustBeforeEach等。

  • BeforeSuite:

    在全部的测试代码执行之前执行的代码

  • AfterSuite:

    在全部的测试代码执行之前执行的代码

  • BeforeEach:

    在块测试代码执行之前执行的代码

  • AfterEach:

    在块测试代码执行之前执行的代码

  • JustBeforeEach:

    和BeforeEach相似,保证在BeforeEach之后运行。再it之前运行

  • JustAfterEach:

    和AfterEach相似,保证再AfterEach之前运行,再it之后运行

三、条件判断

  • Ω:包装了一个实际的值可以返回一个Assertion,允许进行对返回值进行断言

  • Expect:包装了一个实际的值可以返回一个Assertion,允许进行对返回值进行断言等价于Ω

  • ExpectWithOffset:和Expect使用方式相同,等价于Expect(...).WithOffset()

demo:

// 执行创建book cr资源,判断结果是否成功 Expect(k8sClient.Create(context.TODO(), book)).To(Succeed())

在gomega包中内置了很多的类似这样的匹配类型如下

package gomega import ( "time" "github.com/google/go-cmp/cmp" "github.com/onsi/gomega/matchers" "github.com/onsi/gomega/types" ) // Equal uses reflect.DeepEqual to compare actual with expected. Equal is strict about // types when performing comparisons. // It is an error for both actual and expected to be nil. Use BeNil() instead. func Equal(expected interface{}) types.GomegaMatcher { return &matchers.EqualMatcher{
      Expected: expected,
   }
} // BeEquivalentTo is more lax than Equal, allowing equality between different types. // This is done by converting actual to have the type of expected before // attempting equality with reflect.DeepEqual. // It is an error for actual and expected to be nil. Use BeNil() instead. func BeEquivalentTo(expected interface{}) types.GomegaMatcher { return &matchers.BeEquivalentToMatcher{
      Expected: expected,
   }
} // BeComparableTo uses gocmp.Equal from github.com/google/go-cmp (instead of reflect.DeepEqual) to perform a deep comparison. // You can pass cmp.Option as options. // It is an error for actual and expected to be nil. Use BeNil() instead. func BeComparableTo(expected interface{}, opts ...cmp.Option) types.GomegaMatcher { return &matchers.BeComparableToMatcher{
      Expected: expected,
      Options: opts,
   }
} // BeIdenticalTo uses the == operator to compare actual with expected. // BeIdenticalTo is strict about types when performing comparisons. // It is an error for both actual and expected to be nil. Use BeNil() instead. func BeIdenticalTo(expected interface{}) types.GomegaMatcher { return &matchers.BeIdenticalToMatcher{
      Expected: expected,
   }
} // BeNil succeeds if actual is nil func BeNil() types.GomegaMatcher { return &matchers.BeNilMatcher{}
} // BeTrue succeeds if actual is true func BeTrue() types.GomegaMatcher { return &matchers.BeTrueMatcher{}
} // BeFalse succeeds if actual is false func BeFalse() types.GomegaMatcher { return &matchers.BeFalseMatcher{}
} // HaveOccurred succeeds if actual is a non-nil error // The typical Go error checking pattern looks like: // // err := SomethingThatMightFail() // Expect(err).ShouldNot(HaveOccurred()) func HaveOccurred() types.GomegaMatcher { return &matchers.HaveOccurredMatcher{}
} // Succeed passes if actual is a nil error // Succeed is intended to be used with functions that return a single error value. Instead of // // err := SomethingThatMightFail() // Expect(err).ShouldNot(HaveOccurred()) // // You can write: // // Expect(SomethingThatMightFail()).Should(Succeed()) // // It is a mistake to use Succeed with a function that has multiple return values. Gomega's Ω and Expect // functions automatically trigger failure if any return values after the first return value are non-zero/non-nil. // This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass. func Succeed() types.GomegaMatcher { return &matchers.SucceedMatcher{}
} // MatchError succeeds if actual is a non-nil error that matches the passed in string/error. // // These are valid use-cases: // // Expect(err).Should(MatchError("an error")) //asserts that err.Error() == "an error" // Expect(err).Should(MatchError(SomeError)) //asserts that err == SomeError (via reflect.DeepEqual) // // It is an error for err to be nil or an object that does not implement the Error interface func MatchError(expected interface{}) types.GomegaMatcher { return &matchers.MatchErrorMatcher{
      Expected: expected,
   }
} // BeClosed succeeds if actual is a closed channel. // It is an error to pass a non-channel to BeClosed, it is also an error to pass nil // // In order to check whether or not the channel is closed, Gomega must try to read from the channel // (even in the `ShouldNot(BeClosed())` case). You should keep this in mind if you wish to make subsequent assertions about // values coming down the channel. // // Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before // asserting that it is closed (it is not possible to detect that a buffered-channel has been closed until all its buffered values are read). // // Finally, as a corollary: it is an error to check whether or not a send-only channel is closed. func BeClosed() types.GomegaMatcher { return &matchers.BeClosedMatcher{}
} // Receive succeeds if there is a value to be received on actual. // Actual must be a channel (and cannot be a send-only channel) -- anything else is an error. // // Receive returns immediately and never blocks: // // - If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass. // // - If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass. // // - If there is something on the channel `c` ready to be read, then Expect(c).Should(Receive()) will pass and Ω(c).ShouldNot(Receive()) will fail. // // If you have a go-routine running in the background that will write to channel `c` you can: // // Eventually(c).Should(Receive()) // // This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`) // // A similar use-case is to assert that no go-routine writes to a channel (for a period of time). You can do this with `Consistently`: // // Consistently(c).ShouldNot(Receive()) // // You can pass `Receive` a matcher. If you do so, it will match the received object against the matcher. For example: // // Expect(c).Should(Receive(Equal("foo"))) // // When given a matcher, `Receive` will always fail if there is nothing to be received on the channel. // // Passing Receive a matcher is especially useful when paired with Eventually: // // Eventually(c).Should(Receive(ContainSubstring("bar"))) // // will repeatedly attempt to pull values out of `c` until a value matching "bar" is received. // // Finally, if you want to have a reference to the value *sent* to the channel you can pass the `Receive` matcher a pointer to a variable of the appropriate type: // // var myThing thing // Eventually(thingChan).Should(Receive(&myThing)) // Expect(myThing.Sprocket).Should(Equal("foo")) // Expect(myThing.IsValid()).Should(BeTrue()) func Receive(args ...interface{}) types.GomegaMatcher { var arg interface{} if len(args) > 0 {
      arg = args[0]
   } return &matchers.ReceiveMatcher{
      Arg: arg,
   }
} // BeSent succeeds if a value can be sent to actual. // Actual must be a channel (and cannot be a receive-only channel) that can sent the type of the value passed into BeSent -- anything else is an error. // In addition, actual must not be closed. // // BeSent never blocks: // // - If the channel `c` is not ready to receive then Expect(c).Should(BeSent("foo")) will fail immediately // - If the channel `c` is eventually ready to receive then Eventually(c).Should(BeSent("foo")) will succeed.. presuming the channel becomes ready to receive before Eventually's timeout // - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately // // Of course, the value is actually sent to the channel. The point of `BeSent` is less to make an assertion about the availability of the channel (which is typically an implementation detail that your test should not be concerned with). // Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends. func BeSent(arg interface{}) types.GomegaMatcher { return &matchers.BeSentMatcher{
      Arg: arg,
   }
} // MatchRegexp succeeds if actual is a string or stringer that matches the // passed-in regexp. Optional arguments can be provided to construct a regexp // via fmt.Sprintf(). func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher { return &matchers.MatchRegexpMatcher{
      Regexp: regexp,
      Args: args,
   }
} // ContainSubstring succeeds if actual is a string or stringer that contains the // passed-in substring. Optional arguments can be provided to construct the substring // via fmt.Sprintf(). func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher { return &matchers.ContainSubstringMatcher{
      Substr: substr,
      Args: args,
   }
} // HavePrefix succeeds if actual is a string or stringer that contains the // passed-in string as a prefix. Optional arguments can be provided to construct // via fmt.Sprintf(). func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher { return &matchers.HavePrefixMatcher{
      Prefix: prefix,
      Args: args,
   }
} // HaveSuffix succeeds if actual is a string or stringer that contains the // passed-in string as a suffix. Optional arguments can be provided to construct // via fmt.Sprintf(). func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher { return &matchers.HaveSuffixMatcher{
      Suffix: suffix,
      Args: args,
   }
} // MatchJSON succeeds if actual is a string or stringer of JSON that matches // the expected JSON. The JSONs are decoded and the resulting objects are compared via // reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter. func MatchJSON(json interface{}) types.GomegaMatcher { return &matchers.MatchJSONMatcher{
      JSONToMatch: json,
   }
} // MatchXML succeeds if actual is a string or stringer of XML that matches // the expected XML. The XMLs are decoded and the resulting objects are compared via // reflect.DeepEqual so things like whitespaces shouldn't matter. func MatchXML(xml interface{}) types.GomegaMatcher { return &matchers.MatchXMLMatcher{
      XMLToMatch: xml,
   }
} // MatchYAML succeeds if actual is a string or stringer of YAML that matches // the expected YAML. The YAML's are decoded and the resulting objects are compared via // reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter. func MatchYAML(yaml interface{}) types.GomegaMatcher { return &matchers.MatchYAMLMatcher{
      YAMLToMatch: yaml,
   }
} // BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice. func BeEmpty() types.GomegaMatcher { return &matchers.BeEmptyMatcher{}
} // HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice. func HaveLen(count int) types.GomegaMatcher { return &matchers.HaveLenMatcher{
      Count: count,
   }
} // HaveCap succeeds if actual has the passed-in capacity. Actual must be of type array, chan, or slice. func HaveCap(count int) types.GomegaMatcher { return &matchers.HaveCapMatcher{
      Count: count,
   }
} // BeZero succeeds if actual is the zero value for its type or if actual is nil. func BeZero() types.GomegaMatcher { return &matchers.BeZeroMatcher{}
} // ContainElement succeeds if actual contains the passed in element. By default // ContainElement() uses Equal() to perform the match, however a matcher can be // passed in instead: // // Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar"))) // // Actual must be an array, slice or map. For maps, ContainElement searches // through the map's values. // // If you want to have a copy of the matching element(s) found you can pass a // pointer to a variable of the appropriate type. If the variable isn't a slice // or map, then exactly one match will be expected and returned. If the variable // is a slice or map, then at least one match is expected and all matches will be // stored in the variable. // // var findings []string // Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings))) func ContainElement(element interface{}, result ...interface{}) types.GomegaMatcher { return &matchers.ContainElementMatcher{
      Element: element,
      Result: result,
   }
} // BeElementOf succeeds if actual is contained in the passed in elements. // BeElementOf() always uses Equal() to perform the match. // When the passed in elements are comprised of a single element that is either an Array or Slice, BeElementOf() behaves // as the reverse of ContainElement() that operates with Equal() to perform the match. // // Expect(2).Should(BeElementOf([]int{1, 2})) // Expect(2).Should(BeElementOf([2]int{1, 2})) // // Otherwise, BeElementOf() provides a syntactic sugar for Or(Equal(_), Equal(_), ...): // // Expect(2).Should(BeElementOf(1, 2)) // // Actual must be typed. func BeElementOf(elements ...interface{}) types.GomegaMatcher { return &matchers.BeElementOfMatcher{
      Elements: elements,
   }
} // BeKeyOf succeeds if actual is contained in the keys of the passed in map. // BeKeyOf() always uses Equal() to perform the match between actual and the map keys. // // Expect("foo").Should(BeKeyOf(map[string]bool{"foo": true, "bar": false})) func BeKeyOf(element interface{}) types.GomegaMatcher { return &matchers.BeKeyOfMatcher{
      Map: element,
   }
} // ConsistOf succeeds if actual contains precisely the elements passed into the matcher. The ordering of the elements does not matter. // By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples: // // Expect([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo")) // Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo")) // Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo"))) // // Actual must be an array, slice or map. For maps, ConsistOf matches against the map's values. // // You typically pass variadic arguments to ConsistOf (as in the examples above). However, if you need to pass in a slice you can provided that it // is the only element passed in to ConsistOf: // // Expect([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"})) // // Note that Go's type system does not allow you to write this as ConsistOf([]string{"FooBar", "Foo"}...) as []string and []interface{} are different types - hence the need for this special rule. func ConsistOf(elements ...interface{}) types.GomegaMatcher { return &matchers.ConsistOfMatcher{
      Elements: elements,
   }
} // ContainElements succeeds if actual contains the passed in elements. The ordering of the elements does not matter. // By default ContainElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples: // // Expect([]string{"Foo", "FooBar"}).Should(ContainElements("FooBar")) // Expect([]string{"Foo", "FooBar"}).Should(ContainElements(ContainSubstring("Bar"), "Foo")) // // Actual must be an array, slice or map. // For maps, ContainElements searches through the map's values. func ContainElements(elements ...interface{}) types.GomegaMatcher { return &matchers.ContainElementsMatcher{
      Elements: elements,
   }
} // HaveEach succeeds if actual solely contains elements that match the passed in element. // Please note that if actual is empty, HaveEach always will succeed. // By default HaveEach() uses Equal() to perform the match, however a // matcher can be passed in instead: // // Expect([]string{"Foo", "FooBar"}).Should(HaveEach(ContainSubstring("Foo"))) // // Actual must be an array, slice or map. // For maps, HaveEach searches through the map's values. func HaveEach(element interface{}) types.GomegaMatcher { return &matchers.HaveEachMatcher{
      Element: element,
   }
} // HaveKey succeeds if actual is a map with the passed in key. // By default HaveKey uses Equal() to perform the match, however a // matcher can be passed in instead: // // Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`))) func HaveKey(key interface{}) types.GomegaMatcher { return &matchers.HaveKeyMatcher{
      Key: key,
   }
} // HaveKeyWithValue succeeds if actual is a map with the passed in key and value. // By default HaveKeyWithValue uses Equal() to perform the match, however a // matcher can be passed in instead: // // Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar")) // Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar")) func HaveKeyWithValue(key interface{}, value interface{}) types.GomegaMatcher { return &matchers.HaveKeyWithValueMatcher{
      Key: key,
      Value: value,
   }
} // HaveField succeeds if actual is a struct and the value at the passed in field // matches the passed in matcher. By default HaveField used Equal() to perform the match, // however a matcher can be passed in in stead. // // The field must be a string that resolves to the name of a field in the struct. Structs can be traversed // using the '.' delimiter. If the field ends with '()' a method named field is assumed to exist on the struct and is invoked. // Such methods must take no arguments and return a single value: // // type Book struct { // Title string // Author Person // } // type Person struct { // FirstName string // LastName string // DOB time.Time // } // Expect(book).To(HaveField("Title", "Les Miserables")) // Expect(book).To(HaveField("Title", ContainSubstring("Les")) // Expect(book).To(HaveField("Author.FirstName", Equal("Victor")) // Expect(book).To(HaveField("Author.DOB.Year()", BeNumerically("<", 1900)) func HaveField(field string, expected interface{}) types.GomegaMatcher { return &matchers.HaveFieldMatcher{
      Field: field,
      Expected: expected,
   }
} // HaveExistingField succeeds if actual is a struct and the specified field // exists. // // HaveExistingField can be combined with HaveField in order to cover use cases // with optional fields. HaveField alone would trigger an error in such situations. // // Expect(MrHarmless).NotTo(And(HaveExistingField("Title"), HaveField("Title", "Supervillain"))) func HaveExistingField(field string) types.GomegaMatcher { return &matchers.HaveExistingFieldMatcher{
      Field: field,
   }
} // HaveValue applies the given matcher to the value of actual, optionally and // repeatedly dereferencing pointers or taking the concrete value of interfaces. // Thus, the matcher will always be applied to non-pointer and non-interface // values only. HaveValue will fail with an error if a pointer or interface is // nil. It will also fail for more than 31 pointer or interface dereferences to // guard against mistakenly applying it to arbitrarily deep linked pointers. // // HaveValue differs from gstruct.PointTo in that it does not expect actual to // be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer // and even interface values. // // actual := 42 // Expect(actual).To(HaveValue(42)) // Expect(&actual).To(HaveValue(42)) func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher { return &matchers.HaveValueMatcher{
      Matcher: matcher,
   }
} // BeNumerically performs numerical assertions in a type-agnostic way. // Actual and expected should be numbers, though the specific type of // number is irrelevant (float32, float64, uint8, etc...). // // There are six, self-explanatory, supported comparators: // // Expect(1.0).Should(BeNumerically("==", 1)) // Expect(1.0).Should(BeNumerically("~", 0.999, 0.01)) // Expect(1.0).Should(BeNumerically(">", 0.9)) // Expect(1.0).Should(BeNumerically(">=", 1.0)) // Expect(1.0).Should(BeNumerically("<", 3)) // Expect(1.0).Should(BeNumerically("<=", 1.0)) func BeNumerically(comparator string, compareTo ...interface{}) types.GomegaMatcher { return &matchers.BeNumericallyMatcher{
      Comparator: comparator,
      CompareTo: compareTo,
   }
} // BeTemporally compares time.Time's like BeNumerically // Actual and expected must be time.Time. The comparators are the same as for BeNumerically // // Expect(time.Now()).Should(BeTemporally(">", time.Time{})) // Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second)) func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher { return &matchers.BeTemporallyMatcher{
      Comparator: comparator,
      CompareTo: compareTo,
      Threshold: threshold,
   }
} // BeAssignableToTypeOf succeeds if actual is assignable to the type of expected. // It will return an error when one of the values is nil. // // Expect(0).Should(BeAssignableToTypeOf(0)) // Same values // Expect(5).Should(BeAssignableToTypeOf(-1)) // different values same type // Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type // Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{})) func BeAssignableToTypeOf(expected interface{}) types.GomegaMatcher { return &matchers.AssignableToTypeOfMatcher{
      Expected: expected,
   }
} // Panic succeeds if actual is a function that, when invoked, panics. // Actual must be a function that takes no arguments and returns no results. func Panic() types.GomegaMatcher { return &matchers.PanicMatcher{}
} // PanicWith succeeds if actual is a function that, when invoked, panics with a specific value. // Actual must be a function that takes no arguments and returns no results. // // By default PanicWith uses Equal() to perform the match, however a // matcher can be passed in instead: // // Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`))) func PanicWith(expected interface{}) types.GomegaMatcher { return &matchers.PanicMatcher{Expected: expected}
} // BeAnExistingFile succeeds if a file exists. // Actual must be a string representing the abs path to the file being checked. func BeAnExistingFile() types.GomegaMatcher { return &matchers.BeAnExistingFileMatcher{}
} // BeARegularFile succeeds if a file exists and is a regular file. // Actual must be a string representing the abs path to the file being checked. func BeARegularFile() types.GomegaMatcher { return &matchers.BeARegularFileMatcher{}
} // BeADirectory succeeds if a file exists and is a directory. // Actual must be a string representing the abs path to the file being checked. func BeADirectory() types.GomegaMatcher { return &matchers.BeADirectoryMatcher{}
} // HaveHTTPStatus succeeds if the Status or StatusCode field of an HTTP response matches. // Actual must be either a *http.Response or *httptest.ResponseRecorder. // Expected must be either an int or a string. // // Expect(resp).Should(HaveHTTPStatus(http.StatusOK)) // asserts that resp.StatusCode == 200 // Expect(resp).Should(HaveHTTPStatus("404 Not Found")) // asserts that resp.Status == "404 Not Found" // Expect(resp).Should(HaveHTTPStatus(http.StatusOK, http.StatusNoContent)) // asserts that resp.StatusCode == 200 || resp.StatusCode == 204 func HaveHTTPStatus(expected ...interface{}) types.GomegaMatcher { return &matchers.HaveHTTPStatusMatcher{Expected: expected}
} // HaveHTTPHeaderWithValue succeeds if the header is found and the value matches. // Actual must be either a *http.Response or *httptest.ResponseRecorder. // Expected must be a string header name, followed by a header value which // can be a string, or another matcher. func HaveHTTPHeaderWithValue(header string, value interface{}) types.GomegaMatcher { return &matchers.HaveHTTPHeaderWithValueMatcher{
      Header: header,
      Value: value,
   }
} // HaveHTTPBody matches if the body matches. // Actual must be either a *http.Response or *httptest.ResponseRecorder. // Expected must be either a string, []byte, or other matcher func HaveHTTPBody(expected interface{}) types.GomegaMatcher { return &matchers.HaveHTTPBodyMatcher{Expected: expected}
} // And succeeds only if all of the given matchers succeed. // The matchers are tried in order, and will fail-fast if one doesn't succeed. // // Expect("hi").To(And(HaveLen(2), Equal("hi")) // // And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions. func And(ms ...types.GomegaMatcher) types.GomegaMatcher { return &matchers.AndMatcher{Matchers: ms}
} // SatisfyAll is an alias for And(). // // Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi"))) func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher { return And(matchers...)
} // Or succeeds if any of the given matchers succeed. // The matchers are tried in order and will return immediately upon the first successful match. // // Expect("hi").To(Or(HaveLen(3), HaveLen(2)) // // And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions. func Or(ms ...types.GomegaMatcher) types.GomegaMatcher { return &matchers.OrMatcher{Matchers: ms}
} // SatisfyAny is an alias for Or(). // // Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2)) func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher { return Or(matchers...)
} // Not negates the given matcher; it succeeds if the given matcher fails. // // Expect(1).To(Not(Equal(2)) // // And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions. func Not(matcher types.GomegaMatcher) types.GomegaMatcher { return &matchers.NotMatcher{Matcher: matcher}
} // WithTransform applies the `transform` to the actual value and matches it against `matcher`. // The given transform must be either a function of one parameter that returns one value or a // function of one parameter that returns two values, where the second value must be of the // error type. // // var plus1 = func(i int) int { return i + 1 } // Expect(1).To(WithTransform(plus1, Equal(2)) // // var failingplus1 = func(i int) (int, error) { return 42, "this does not compute" } // Expect(1).To(WithTransform(failingplus1, Equal(2))) // // And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions. func WithTransform(transform interface{}, matcher types.GomegaMatcher) types.GomegaMatcher { return matchers.NewWithTransformMatcher(transform, matcher)
} // Satisfy matches the actual value against the `predicate` function. // The given predicate must be a function of one paramter that returns bool. // // var isEven = func(i int) bool { return i%2 == 0 } // Expect(2).To(Satisfy(isEven)) func Satisfy(predicate interface{}) types.GomegaMatcher { return matchers.NewSatisfyMatcher(predicate)
}

我们可以自己实际的情况来选择最终的预期结果来进行判断:

如预期的结果是一个比较复杂的结构体对象,则可以使用Equal方法来判断两个结构体是否相当,而Equal中实际使用的则是reflect.DeepEqual方法。

四、循环判断

在使用的测试用例的编写过程中,我们可能会遇到需要循环去获取集群中的某个资源的状态,并在预期的时间内达到预期状态,我们仍然可以通过gomega包中的一些方法来实现:

  • Eventually
    循环执行传入的函数。根据传入的时间间隔和超时时间循环执行并再超过超时时间后失败,判断传入函数是否复合matcher
  • EventuallyWithOffset:
    循环执行传入的函数,根据传入的时间间隔和超时时间循环执行并再超过超时时间后失败,判断传入函数是否复合matcher,等价于Event-ually(...).WithOffset(...)
  • Consistently:
    和Eventually使用方式相同,但是支持异步判断
  • ConsistentlyWithOffset:
    与Consistently使用方式相同等价于Consistently(...).WithOffset(...)

demo:

//持续获取book资源直到获取资源的error为nil Eventually(func() error{ return k8sClient.Get(context.TODO(), types.NamespacedName{
      Name: Name,
      Namespace: Namespace,
   }, &demov1.Demobook{})
},time.Second).Should(BeNil())

本次分享就到这里啦~

Hello~

这里是神州数码云基地
编程大法,技术前沿,尽在其中
Odoo、数据库、云原生、DevOps
超多原创技术干货持续输出ing~

原文链接: http://mp.weixin.qq.com/s?__biz=Mzg5MzUyOTgwMQ==&mid=2247522709&idx=1&sn=ee830d650eae14d6c5e33d5d99f9c27c&chksm=c02fa233f7582b258e00014288b25a61b422e0881cdb5d733df8abf65017e1261c1690fa1ba3#rd