...

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/v2"
    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  								JVMClassMethodSpec: JVMClassMethodSpec{
    56  									Class:  "Main",
    57  									Method: "print",
    58  								},
    59  								LatencyDuration: 1000,
    60  							},
    61  						},
    62  					},
    63  					execute: func(chaos *JVMChaos) error {
    64  						_, err := chaos.ValidateCreate()
    65  						return err
    66  					},
    67  					expect: "",
    68  				},
    69  				{
    70  					name: "simple ValidateUpdate",
    71  					chaos: JVMChaos{
    72  						ObjectMeta: metav1.ObjectMeta{
    73  							Namespace: metav1.NamespaceDefault,
    74  							Name:      "foo2",
    75  						},
    76  						Spec: JVMChaosSpec{
    77  							Action: JVMLatencyAction,
    78  							JVMParameter: JVMParameter{
    79  								JVMClassMethodSpec: JVMClassMethodSpec{
    80  									Class:  "Main",
    81  									Method: "print",
    82  								},
    83  								LatencyDuration: 1000,
    84  							},
    85  						},
    86  					},
    87  					execute: func(chaos *JVMChaos) error {
    88  						_, err := chaos.ValidateUpdate(chaos)
    89  						return err
    90  					},
    91  					expect: "",
    92  				},
    93  				{
    94  					name: "simple ValidateDelete",
    95  					chaos: JVMChaos{
    96  						ObjectMeta: metav1.ObjectMeta{
    97  							Namespace: metav1.NamespaceDefault,
    98  							Name:      "foo3",
    99  						},
   100  						Spec: JVMChaosSpec{
   101  							Action: JVMLatencyAction,
   102  							JVMParameter: JVMParameter{
   103  								JVMClassMethodSpec: JVMClassMethodSpec{
   104  									Class:  "Main",
   105  									Method: "print",
   106  								},
   107  								LatencyDuration: 1000,
   108  							},
   109  						},
   110  					},
   111  					execute: func(chaos *JVMChaos) error {
   112  						_, err := chaos.ValidateDelete()
   113  						return err
   114  					},
   115  					expect: "",
   116  				},
   117  				{
   118  					name: "missing latency",
   119  					chaos: JVMChaos{
   120  						ObjectMeta: metav1.ObjectMeta{
   121  							Namespace: metav1.NamespaceDefault,
   122  							Name:      "foo4",
   123  						},
   124  						Spec: JVMChaosSpec{
   125  							Action: JVMLatencyAction,
   126  							JVMParameter: JVMParameter{
   127  								JVMClassMethodSpec: JVMClassMethodSpec{
   128  									Class:  "Main",
   129  									Method: "print",
   130  								},
   131  							},
   132  						},
   133  					},
   134  					execute: func(chaos *JVMChaos) error {
   135  						_, err := chaos.ValidateCreate()
   136  						return err
   137  					},
   138  					expect: "error",
   139  				},
   140  				{
   141  					name: "missing value",
   142  					chaos: JVMChaos{
   143  						ObjectMeta: metav1.ObjectMeta{
   144  							Namespace: metav1.NamespaceDefault,
   145  							Name:      "foo5",
   146  						},
   147  						Spec: JVMChaosSpec{
   148  							Action: JVMReturnAction,
   149  							JVMParameter: JVMParameter{
   150  								JVMClassMethodSpec: JVMClassMethodSpec{
   151  									Class:  "Main",
   152  									Method: "print",
   153  								},
   154  							},
   155  						},
   156  					},
   157  					execute: func(chaos *JVMChaos) error {
   158  						_, err := chaos.ValidateCreate()
   159  						return err
   160  					},
   161  					expect: "error",
   162  				},
   163  				{
   164  					name: "missing exception",
   165  					chaos: JVMChaos{
   166  						ObjectMeta: metav1.ObjectMeta{
   167  							Namespace: metav1.NamespaceDefault,
   168  							Name:      "foo6",
   169  						},
   170  						Spec: JVMChaosSpec{
   171  							Action: JVMExceptionAction,
   172  							JVMParameter: JVMParameter{
   173  								JVMClassMethodSpec: JVMClassMethodSpec{
   174  									Class:  "Main",
   175  									Method: "print",
   176  								},
   177  							},
   178  						},
   179  					},
   180  					execute: func(chaos *JVMChaos) error {
   181  						_, err := chaos.ValidateCreate()
   182  						return err
   183  					},
   184  					expect: "error",
   185  				},
   186  				{
   187  					name: "missing class",
   188  					chaos: JVMChaos{
   189  						ObjectMeta: metav1.ObjectMeta{
   190  							Namespace: metav1.NamespaceDefault,
   191  							Name:      "foo7",
   192  						},
   193  						Spec: JVMChaosSpec{
   194  							Action: JVMLatencyAction,
   195  							JVMParameter: JVMParameter{
   196  								JVMClassMethodSpec: JVMClassMethodSpec{
   197  									Method: "print",
   198  								},
   199  								LatencyDuration: 1000,
   200  							},
   201  						},
   202  					},
   203  					execute: func(chaos *JVMChaos) error {
   204  						_, err := chaos.ValidateCreate()
   205  						return err
   206  					},
   207  					expect: "error",
   208  				},
   209  				{
   210  					name: "missing method",
   211  					chaos: JVMChaos{
   212  						ObjectMeta: metav1.ObjectMeta{
   213  							Namespace: metav1.NamespaceDefault,
   214  							Name:      "foo8",
   215  						},
   216  						Spec: JVMChaosSpec{
   217  							Action: JVMLatencyAction,
   218  							JVMParameter: JVMParameter{
   219  								JVMClassMethodSpec: JVMClassMethodSpec{
   220  									Class: "Main",
   221  								},
   222  								LatencyDuration: 1000,
   223  							},
   224  						},
   225  					},
   226  					execute: func(chaos *JVMChaos) error {
   227  						_, err := chaos.ValidateCreate()
   228  						return err
   229  					},
   230  					expect: "error",
   231  				},
   232  				{
   233  					name: "missing rule data",
   234  					chaos: JVMChaos{
   235  						ObjectMeta: metav1.ObjectMeta{
   236  							Namespace: metav1.NamespaceDefault,
   237  							Name:      "foo9",
   238  						},
   239  						Spec: JVMChaosSpec{
   240  							Action: JVMRuleDataAction,
   241  						},
   242  					},
   243  					execute: func(chaos *JVMChaos) error {
   244  						_, err := chaos.ValidateCreate()
   245  						return err
   246  					},
   247  					expect: "error",
   248  				},
   249  				{
   250  					name: "missing cpu-count and memory type",
   251  					chaos: JVMChaos{
   252  						ObjectMeta: metav1.ObjectMeta{
   253  							Namespace: metav1.NamespaceDefault,
   254  							Name:      "foo10",
   255  						},
   256  						Spec: JVMChaosSpec{
   257  							Action: JVMStressAction,
   258  						},
   259  					},
   260  					execute: func(chaos *JVMChaos) error {
   261  						_, err := chaos.ValidateCreate()
   262  						return err
   263  					},
   264  					expect: "error",
   265  				},
   266  			}
   267  
   268  			for _, tc := range tcs {
   269  				err := tc.execute(&tc.chaos)
   270  				if tc.expect == "error" {
   271  					Expect(err).To(HaveOccurred())
   272  				} else {
   273  					Expect(err).NotTo(HaveOccurred())
   274  				}
   275  			}
   276  		})
   277  	})
   278  })
   279