...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/jvmchaos_webhook_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     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 v1alpha1
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  var _ = Describe("jvmchaos_webhook", func() {
    25  	Context("Defaulter", func() {
    26  		It("set default namespace selector", func() {
    27  			jvmchaos := &JVMChaos{
    28  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    29  			}
    30  			jvmchaos.Default()
    31  			Expect(jvmchaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    32  		})
    33  	})
    34  	Context("webhook.Validator of jvmchaos", func() {
    35  		It("Validate JVMChaos", func() {
    36  
    37  			type TestCase struct {
    38  				name    string
    39  				chaos   JVMChaos
    40  				execute func(chaos *JVMChaos) error
    41  				expect  string
    42  			}
    43  
    44  			tcs := []TestCase{
    45  				{
    46  					name: "simple ValidateCreate",
    47  					chaos: JVMChaos{
    48  						ObjectMeta: metav1.ObjectMeta{
    49  							Namespace: metav1.NamespaceDefault,
    50  							Name:      "foo1",
    51  						},
    52  						Spec: JVMChaosSpec{
    53  							Action: JVMLatencyAction,
    54  							JVMParameter: JVMParameter{
    55  								Class:           "Main",
    56  								Method:          "print",
    57  								LatencyDuration: 1000,
    58  							},
    59  						},
    60  					},
    61  					execute: func(chaos *JVMChaos) error {
    62  						return chaos.ValidateCreate()
    63  					},
    64  					expect: "",
    65  				},
    66  				{
    67  					name: "simple ValidateUpdate",
    68  					chaos: JVMChaos{
    69  						ObjectMeta: metav1.ObjectMeta{
    70  							Namespace: metav1.NamespaceDefault,
    71  							Name:      "foo2",
    72  						},
    73  						Spec: JVMChaosSpec{
    74  							Action: JVMLatencyAction,
    75  							JVMParameter: JVMParameter{
    76  								Class:           "Main",
    77  								Method:          "print",
    78  								LatencyDuration: 1000,
    79  							},
    80  						},
    81  					},
    82  					execute: func(chaos *JVMChaos) error {
    83  						return chaos.ValidateUpdate(chaos)
    84  					},
    85  					expect: "",
    86  				},
    87  				{
    88  					name: "simple ValidateDelete",
    89  					chaos: JVMChaos{
    90  						ObjectMeta: metav1.ObjectMeta{
    91  							Namespace: metav1.NamespaceDefault,
    92  							Name:      "foo3",
    93  						},
    94  						Spec: JVMChaosSpec{
    95  							Action: JVMLatencyAction,
    96  							JVMParameter: JVMParameter{
    97  								Class:           "Main",
    98  								Method:          "print",
    99  								LatencyDuration: 1000,
   100  							},
   101  						},
   102  					},
   103  					execute: func(chaos *JVMChaos) error {
   104  						return chaos.ValidateDelete()
   105  					},
   106  					expect: "",
   107  				},
   108  				{
   109  					name: "missing latency",
   110  					chaos: JVMChaos{
   111  						ObjectMeta: metav1.ObjectMeta{
   112  							Namespace: metav1.NamespaceDefault,
   113  							Name:      "foo4",
   114  						},
   115  						Spec: JVMChaosSpec{
   116  							Action: JVMLatencyAction,
   117  							JVMParameter: JVMParameter{
   118  								Class:  "Main",
   119  								Method: "print",
   120  							},
   121  						},
   122  					},
   123  					execute: func(chaos *JVMChaos) error {
   124  						return chaos.ValidateCreate()
   125  					},
   126  					expect: "error",
   127  				},
   128  				{
   129  					name: "missing value",
   130  					chaos: JVMChaos{
   131  						ObjectMeta: metav1.ObjectMeta{
   132  							Namespace: metav1.NamespaceDefault,
   133  							Name:      "foo5",
   134  						},
   135  						Spec: JVMChaosSpec{
   136  							Action: JVMReturnAction,
   137  							JVMParameter: JVMParameter{
   138  								Class:  "Main",
   139  								Method: "print",
   140  							},
   141  						},
   142  					},
   143  					execute: func(chaos *JVMChaos) error {
   144  						return chaos.ValidateCreate()
   145  					},
   146  					expect: "error",
   147  				},
   148  				{
   149  					name: "missing exception",
   150  					chaos: JVMChaos{
   151  						ObjectMeta: metav1.ObjectMeta{
   152  							Namespace: metav1.NamespaceDefault,
   153  							Name:      "foo6",
   154  						},
   155  						Spec: JVMChaosSpec{
   156  							Action: JVMExceptionAction,
   157  							JVMParameter: JVMParameter{
   158  								Class:  "Main",
   159  								Method: "print",
   160  							},
   161  						},
   162  					},
   163  					execute: func(chaos *JVMChaos) error {
   164  						return chaos.ValidateCreate()
   165  					},
   166  					expect: "error",
   167  				},
   168  				{
   169  					name: "missing class",
   170  					chaos: JVMChaos{
   171  						ObjectMeta: metav1.ObjectMeta{
   172  							Namespace: metav1.NamespaceDefault,
   173  							Name:      "foo7",
   174  						},
   175  						Spec: JVMChaosSpec{
   176  							Action: JVMLatencyAction,
   177  							JVMParameter: JVMParameter{
   178  								Method:          "print",
   179  								LatencyDuration: 1000,
   180  							},
   181  						},
   182  					},
   183  					execute: func(chaos *JVMChaos) error {
   184  						return chaos.ValidateCreate()
   185  					},
   186  					expect: "error",
   187  				},
   188  				{
   189  					name: "missing method",
   190  					chaos: JVMChaos{
   191  						ObjectMeta: metav1.ObjectMeta{
   192  							Namespace: metav1.NamespaceDefault,
   193  							Name:      "foo8",
   194  						},
   195  						Spec: JVMChaosSpec{
   196  							Action: JVMLatencyAction,
   197  							JVMParameter: JVMParameter{
   198  								Class:           "Main",
   199  								LatencyDuration: 1000,
   200  							},
   201  						},
   202  					},
   203  					execute: func(chaos *JVMChaos) error {
   204  						return chaos.ValidateCreate()
   205  					},
   206  					expect: "error",
   207  				},
   208  				{
   209  					name: "missing rule data",
   210  					chaos: JVMChaos{
   211  						ObjectMeta: metav1.ObjectMeta{
   212  							Namespace: metav1.NamespaceDefault,
   213  							Name:      "foo9",
   214  						},
   215  						Spec: JVMChaosSpec{
   216  							Action: JVMRuleDataAction,
   217  						},
   218  					},
   219  					execute: func(chaos *JVMChaos) error {
   220  						return chaos.ValidateCreate()
   221  					},
   222  					expect: "error",
   223  				},
   224  				{
   225  					name: "missing cpu-count and memory type",
   226  					chaos: JVMChaos{
   227  						ObjectMeta: metav1.ObjectMeta{
   228  							Namespace: metav1.NamespaceDefault,
   229  							Name:      "foo10",
   230  						},
   231  						Spec: JVMChaosSpec{
   232  							Action: JVMStressAction,
   233  						},
   234  					},
   235  					execute: func(chaos *JVMChaos) error {
   236  						return chaos.ValidateCreate()
   237  					},
   238  					expect: "error",
   239  				},
   240  			}
   241  
   242  			for _, tc := range tcs {
   243  				err := tc.execute(&tc.chaos)
   244  				if tc.expect == "error" {
   245  					Expect(err).To(HaveOccurred())
   246  				} else {
   247  					Expect(err).NotTo(HaveOccurred())
   248  				}
   249  			}
   250  		})
   251  	})
   252  })
   253