How to use Make method of state Package

Best Syzkaller code snippet using state.Make

kuberuntime_gc_test.go

Source:kuberuntime_gc_test.go Github

copy

Full Screen

1/*2Copyright 2016 The Kubernetes Authors.3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/13package kuberuntime14import (15 "os"16 "path/filepath"17 "testing"18 "time"19 "github.com/golang/mock/gomock"20 "github.com/stretchr/testify/assert"21 "k8s.io/api/core/v1"22 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"23 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"24 containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"25)26func TestSandboxGC(t *testing.T) {27 fakeRuntime, _, m, err := createTestRuntimeManager()28 assert.NoError(t, err)29 podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)30 makeGCSandbox := func(pod *v1.Pod, attempt uint32, state runtimeapi.PodSandboxState, withPodStateProvider bool, createdAt int64) sandboxTemplate {31 if withPodStateProvider {32 // initialize the pod getter33 podStateProvider.existingPods[pod.UID] = struct{}{}34 }35 return sandboxTemplate{36 pod: pod,37 state: state,38 attempt: attempt,39 createdAt: createdAt,40 }41 }42 pods := []*v1.Pod{43 makeTestPod("foo1", "new", "1234", []v1.Container{44 makeTestContainer("bar1", "busybox"),45 makeTestContainer("bar2", "busybox"),46 }),47 makeTestPod("foo2", "new", "5678", []v1.Container{48 makeTestContainer("bar3", "busybox"),49 }),50 makeTestPod("deleted", "new", "9012", []v1.Container{51 makeTestContainer("bar4", "busybox"),52 }),53 }54 for c, test := range []struct {55 description string // description of the test case56 sandboxes []sandboxTemplate // templates of sandboxes57 containers []containerTemplate // templates of containers58 remain []int // template indexes of remaining sandboxes59 evictTerminatedPods bool60 }{61 {62 description: "notready sandboxes without containers for deleted pods should be garbage collected.",63 sandboxes: []sandboxTemplate{64 makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),65 },66 containers: []containerTemplate{},67 remain: []int{},68 evictTerminatedPods: false,69 },70 {71 description: "ready sandboxes without containers for deleted pods should not be garbage collected.",72 sandboxes: []sandboxTemplate{73 makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_READY, false, 0),74 },75 containers: []containerTemplate{},76 remain: []int{0},77 evictTerminatedPods: false,78 },79 {80 description: "sandboxes for existing pods should not be garbage collected.",81 sandboxes: []sandboxTemplate{82 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_READY, true, 0),83 makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),84 },85 containers: []containerTemplate{},86 remain: []int{0, 1},87 evictTerminatedPods: false,88 },89 {90 description: "older exited sandboxes without containers for existing pods should be garbage collected if there are more than one exited sandboxes.",91 sandboxes: []sandboxTemplate{92 makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),93 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),94 },95 containers: []containerTemplate{},96 remain: []int{0},97 evictTerminatedPods: false,98 },99 {100 description: "older exited sandboxes with containers for existing pods should not be garbage collected even if there are more than one exited sandboxes.",101 sandboxes: []sandboxTemplate{102 makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),103 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),104 },105 containers: []containerTemplate{106 {pod: pods[0], container: &pods[0].Spec.Containers[0], sandboxAttempt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},107 },108 remain: []int{0, 1},109 evictTerminatedPods: false,110 },111 {112 description: "non-running sandboxes for existing pods should be garbage collected if evictTerminatedPods is set.",113 sandboxes: []sandboxTemplate{114 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_READY, true, 0),115 makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),116 },117 containers: []containerTemplate{},118 remain: []int{0},119 evictTerminatedPods: true,120 },121 {122 description: "sandbox with containers should not be garbage collected.",123 sandboxes: []sandboxTemplate{124 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),125 },126 containers: []containerTemplate{127 {pod: pods[0], container: &pods[0].Spec.Containers[0], state: runtimeapi.ContainerState_CONTAINER_EXITED},128 },129 remain: []int{0},130 evictTerminatedPods: false,131 },132 {133 description: "multiple sandboxes should be handled properly.",134 sandboxes: []sandboxTemplate{135 // running sandbox.136 makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_READY, true, 1),137 // exited sandbox without containers.138 makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),139 // exited sandbox with containers.140 makeGCSandbox(pods[1], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),141 // exited sandbox without containers.142 makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),143 // exited sandbox without containers for deleted pods.144 makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),145 },146 containers: []containerTemplate{147 {pod: pods[1], container: &pods[1].Spec.Containers[0], sandboxAttempt: 1, state: runtimeapi.ContainerState_CONTAINER_EXITED},148 },149 remain: []int{0, 2},150 evictTerminatedPods: false,151 },152 } {153 t.Logf("TestCase #%d: %+v", c, test)154 fakeSandboxes := makeFakePodSandboxes(t, m, test.sandboxes)155 fakeContainers := makeFakeContainers(t, m, test.containers)156 fakeRuntime.SetFakeSandboxes(fakeSandboxes)157 fakeRuntime.SetFakeContainers(fakeContainers)158 err := m.containerGC.evictSandboxes(test.evictTerminatedPods)159 assert.NoError(t, err)160 realRemain, err := fakeRuntime.ListPodSandbox(nil)161 assert.NoError(t, err)162 assert.Len(t, realRemain, len(test.remain))163 for _, remain := range test.remain {164 status, err := fakeRuntime.PodSandboxStatus(fakeSandboxes[remain].Id)165 assert.NoError(t, err)166 assert.Equal(t, &fakeSandboxes[remain].PodSandboxStatus, status)167 }168 }169}170func makeGCContainer(podStateProvider *fakePodStateProvider, podName, containerName string, attempt int, createdAt int64, state runtimeapi.ContainerState) containerTemplate {171 container := makeTestContainer(containerName, "test-image")172 pod := makeTestPod(podName, "test-ns", podName, []v1.Container{container})173 if podName == "running" {174 podStateProvider.runningPods[pod.UID] = struct{}{}175 }176 if podName != "deleted" {177 podStateProvider.existingPods[pod.UID] = struct{}{}178 }179 return containerTemplate{180 pod: pod,181 container: &container,182 attempt: attempt,183 createdAt: createdAt,184 state: state,185 }186}187func TestContainerGC(t *testing.T) {188 fakeRuntime, _, m, err := createTestRuntimeManager()189 assert.NoError(t, err)190 podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)191 defaultGCPolicy := kubecontainer.ContainerGCPolicy{MinAge: time.Hour, MaxPerPodContainer: 2, MaxContainers: 6}192 for c, test := range []struct {193 description string // description of the test case194 containers []containerTemplate // templates of containers195 policy *kubecontainer.ContainerGCPolicy // container gc policy196 remain []int // template indexes of remaining containers197 evictTerminatedPods bool198 allSourcesReady bool199 }{200 {201 description: "all containers should be removed when max container limit is 0",202 containers: []containerTemplate{203 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),204 },205 policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: 1, MaxContainers: 0},206 remain: []int{},207 evictTerminatedPods: false,208 allSourcesReady: true,209 },210 {211 description: "max containers should be complied when no max per pod container limit is set",212 containers: []containerTemplate{213 makeGCContainer(podStateProvider, "foo", "bar", 4, 4, runtimeapi.ContainerState_CONTAINER_EXITED),214 makeGCContainer(podStateProvider, "foo", "bar", 3, 3, runtimeapi.ContainerState_CONTAINER_EXITED),215 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),216 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),217 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),218 },219 policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: -1, MaxContainers: 4},220 remain: []int{0, 1, 2, 3},221 evictTerminatedPods: false,222 allSourcesReady: true,223 },224 {225 description: "no containers should be removed if both max container and per pod container limits are not set",226 containers: []containerTemplate{227 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),228 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),229 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),230 },231 policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: -1, MaxContainers: -1},232 remain: []int{0, 1, 2},233 evictTerminatedPods: false,234 allSourcesReady: true,235 },236 {237 description: "recently started containers should not be removed",238 containers: []containerTemplate{239 makeGCContainer(podStateProvider, "foo", "bar", 2, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),240 makeGCContainer(podStateProvider, "foo", "bar", 1, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),241 makeGCContainer(podStateProvider, "foo", "bar", 0, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),242 },243 remain: []int{0, 1, 2},244 evictTerminatedPods: false,245 allSourcesReady: true,246 },247 {248 description: "oldest containers should be removed when per pod container limit exceeded",249 containers: []containerTemplate{250 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),251 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),252 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),253 },254 remain: []int{0, 1},255 evictTerminatedPods: false,256 allSourcesReady: true,257 },258 {259 description: "running containers should not be removed",260 containers: []containerTemplate{261 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),262 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),263 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_RUNNING),264 },265 remain: []int{0, 1, 2},266 evictTerminatedPods: false,267 allSourcesReady: true,268 },269 {270 description: "no containers should be removed when limits are not exceeded",271 containers: []containerTemplate{272 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),273 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),274 },275 remain: []int{0, 1},276 evictTerminatedPods: false,277 allSourcesReady: true,278 },279 {280 description: "max container count should apply per (UID, container) pair",281 containers: []containerTemplate{282 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),283 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),284 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),285 makeGCContainer(podStateProvider, "foo1", "baz", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),286 makeGCContainer(podStateProvider, "foo1", "baz", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),287 makeGCContainer(podStateProvider, "foo1", "baz", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),288 makeGCContainer(podStateProvider, "foo2", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),289 makeGCContainer(podStateProvider, "foo2", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),290 makeGCContainer(podStateProvider, "foo2", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),291 },292 remain: []int{0, 1, 3, 4, 6, 7},293 evictTerminatedPods: false,294 allSourcesReady: true,295 },296 {297 description: "max limit should apply and try to keep from every pod",298 containers: []containerTemplate{299 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),300 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),301 makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),302 makeGCContainer(podStateProvider, "foo1", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),303 makeGCContainer(podStateProvider, "foo2", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),304 makeGCContainer(podStateProvider, "foo2", "bar2", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),305 makeGCContainer(podStateProvider, "foo3", "bar3", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),306 makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),307 makeGCContainer(podStateProvider, "foo4", "bar4", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),308 makeGCContainer(podStateProvider, "foo4", "bar4", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),309 },310 remain: []int{0, 2, 4, 6, 8},311 evictTerminatedPods: false,312 allSourcesReady: true,313 },314 {315 description: "oldest pods should be removed if limit exceeded",316 containers: []containerTemplate{317 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),318 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),319 makeGCContainer(podStateProvider, "foo1", "bar1", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),320 makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),321 makeGCContainer(podStateProvider, "foo2", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),322 makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),323 makeGCContainer(podStateProvider, "foo4", "bar4", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),324 makeGCContainer(podStateProvider, "foo5", "bar5", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),325 makeGCContainer(podStateProvider, "foo6", "bar6", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),326 makeGCContainer(podStateProvider, "foo7", "bar7", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),327 },328 remain: []int{0, 2, 4, 6, 8, 9},329 evictTerminatedPods: false,330 allSourcesReady: true,331 },332 {333 description: "all non-running containers should be removed when evictTerminatedPods is set",334 containers: []containerTemplate{335 makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),336 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),337 makeGCContainer(podStateProvider, "foo1", "bar1", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),338 makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),339 makeGCContainer(podStateProvider, "running", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),340 makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_RUNNING),341 },342 remain: []int{4, 5},343 evictTerminatedPods: true,344 allSourcesReady: true,345 },346 {347 description: "containers for deleted pods should be removed",348 containers: []containerTemplate{349 makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),350 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),351 // deleted pods still respect MinAge.352 makeGCContainer(podStateProvider, "deleted", "bar1", 2, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),353 makeGCContainer(podStateProvider, "deleted", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),354 makeGCContainer(podStateProvider, "deleted", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),355 },356 remain: []int{0, 1, 2},357 evictTerminatedPods: false,358 allSourcesReady: true,359 },360 {361 description: "containers for deleted pods may not be removed if allSourcesReady is set false ",362 containers: []containerTemplate{363 makeGCContainer(podStateProvider, "deleted", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),364 },365 remain: []int{0},366 evictTerminatedPods: true,367 allSourcesReady: false,368 },369 } {370 t.Logf("TestCase #%d: %+v", c, test)371 fakeContainers := makeFakeContainers(t, m, test.containers)372 fakeRuntime.SetFakeContainers(fakeContainers)373 if test.policy == nil {374 test.policy = &defaultGCPolicy375 }376 err := m.containerGC.evictContainers(*test.policy, test.allSourcesReady, test.evictTerminatedPods)377 assert.NoError(t, err)378 realRemain, err := fakeRuntime.ListContainers(nil)379 assert.NoError(t, err)380 assert.Len(t, realRemain, len(test.remain))381 for _, remain := range test.remain {382 status, err := fakeRuntime.ContainerStatus(fakeContainers[remain].Id)383 assert.NoError(t, err)384 assert.Equal(t, &fakeContainers[remain].ContainerStatus, status)385 }386 }387}388// Notice that legacy container symlink is not tested since it may be deprecated soon.389func TestPodLogDirectoryGC(t *testing.T) {390 _, _, m, err := createTestRuntimeManager()391 assert.NoError(t, err)392 fakeOS := m.osInterface.(*containertest.FakeOS)393 podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)394 // pod log directories without corresponding pods should be removed.395 podStateProvider.existingPods["123"] = struct{}{}396 podStateProvider.existingPods["456"] = struct{}{}397 podStateProvider.existingPods["321"] = struct{}{}398 podStateProvider.runningPods["123"] = struct{}{}399 podStateProvider.runningPods["456"] = struct{}{}400 podStateProvider.existingPods["321"] = struct{}{}401 files := []string{"123", "456", "789", "012", "name_namespace_321", "name_namespace_654"}402 removed := []string{403 filepath.Join(podLogsRootDirectory, "789"),404 filepath.Join(podLogsRootDirectory, "012"),405 filepath.Join(podLogsRootDirectory, "name_namespace_654"),406 }407 ctrl := gomock.NewController(t)408 defer ctrl.Finish()409 fakeOS.ReadDirFn = func(string) ([]os.FileInfo, error) {410 var fileInfos []os.FileInfo411 for _, file := range files {412 mockFI := containertest.NewMockFileInfo(ctrl)413 mockFI.EXPECT().Name().Return(file)414 fileInfos = append(fileInfos, mockFI)415 }416 return fileInfos, nil417 }418 // allSourcesReady == true, pod log directories without corresponding pod should be removed.419 err = m.containerGC.evictPodLogsDirectories(true)420 assert.NoError(t, err)421 assert.Equal(t, removed, fakeOS.Removes)422 // allSourcesReady == false, pod log directories should not be removed.423 fakeOS.Removes = []string{}424 err = m.containerGC.evictPodLogsDirectories(false)425 assert.NoError(t, err)426 assert.Empty(t, fakeOS.Removes)427}428func TestUnknownStateContainerGC(t *testing.T) {429 fakeRuntime, _, m, err := createTestRuntimeManager()430 assert.NoError(t, err)431 podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)432 defaultGCPolicy := kubecontainer.ContainerGCPolicy{MinAge: time.Hour, MaxPerPodContainer: 0, MaxContainers: 0}433 fakeContainers := makeFakeContainers(t, m, []containerTemplate{434 makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_UNKNOWN),435 })436 fakeRuntime.SetFakeContainers(fakeContainers)437 err = m.containerGC.evictContainers(defaultGCPolicy, true, false)438 assert.NoError(t, err)439 assert.Contains(t, fakeRuntime.GetCalls(), "StopContainer", "RemoveContainer",440 "container in unknown state should be stopped before being removed")441 remain, err := fakeRuntime.ListContainers(nil)442 assert.NoError(t, err)443 assert.Empty(t, remain)444}...

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2type State struct {3}4func (s *State) Make(name string, capital string) *State {5}6func main() {7 state := new(State)8 state.Make("California", "Sacramento")9 fmt.Println(state)10}11import (12type State struct {13}14func (s *State) Make(name string, capital string) *State {15}16func main() {17 state := new(State).Make("California", "Sacramento")18 fmt.Println(state)19}20import (21type State struct {22}23func (s *State) Make(name string, capital string) *State {24}25func main() {26 state := new(State)27 state.Make("California", "Sacramento")28 fmt.Println(state)29}30import (31type State struct {32}33func (s *State) Make(name string, capital string) *State {34}35func main() {36 state := new(State).Make("California", "Sacramento")37 fmt.Println(state)38}39import (40type State struct {41}42func (s *State) Make(name string, capital string) *State {43}44func main() {

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2type State struct {3}4func (s *State) Make() string {5}6func main() {7 s := State{"Tamil Nadu"}8 fmt.Println(s.Make())9}

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2type State struct {3}4func (s *State) Make() {5 fmt.Println("Make method called")6}7func main() {8 s := State{Name: "Maharashtra"}9 s.Make()10}11import (12type State struct {13}14func (s *State) Make() {15 fmt.Println("Make method called")16}17func main() {18 s := State{Name: "Maharashtra"}19 s.Make()20}21import (22type State struct {23}24func (s *State) Make() {25 fmt.Println("Make method called")26}27func main() {28 s := State{Name: "Maharashtra"}29 s.Make()30}31import (32type State struct {33}34func (s *State) Make() {35 fmt.Println("Make method called")36}37func main() {38 s := State{Name: "Maharashtra"}39 s.Make()40}41import (42type State struct {43}44func (s *State) Make() {45 fmt.Println("Make method called")46}47func main() {48 s := State{Name: "Maharashtra"}49 s.Make()50}51import (52type State struct {53}54func (s *State) Make() {55 fmt.Println("Make method called")56}57func main() {58 s := State{Name: "Maharashtra"}59 s.Make()60}61import (62type State struct {63}64func (s *State) Make() {65 fmt.Println("Make method called")66}67func main() {68 s := State{Name

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import "fmt"2type state struct {3}4func main() {5 cali := state {6 }7 fmt.Println(cali)8}9import "fmt"10type state struct {11}12func main() {13 cali := state {14 }15 fmt.Println(cali)16 texas := state {17 }18 fmt.Println(texas)19}20import "fmt"21type state struct {22}23func main() {24 cali := state {25 }26 fmt.Println(cali)27 texas := state {28 }29 fmt.Println(texas)30 florida := state {31 }32 fmt.Println(florida)33}34import "fmt"35type state struct {36}37func main() {

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 state := State{}4 city := City{}5 country := Country{}6 state.Make("California", "CA")7 city.Make("San Francisco", "SF")8 country.Make("United States of America", "USA")9 fmt.Println(state)10 fmt.Println(city)11 fmt.Println(country)12}13import (14func main() {15 state := State{}16 city := City{}17 country := Country{}18 state.Make("California", "CA")19 city.Make("San Francisco", "SF")20 country.Make("United States of America", "USA")21 fmt.Println(state)22 fmt.Println(city)23 fmt.Println(country)24}25import (26func main() {27 state := State{}28 city := City{}29 country := Country{}30 state.Make("California", "CA")31 city.Make("San Francisco", "SF")32 country.Make("United States of America", "USA")33 fmt.Println(state)34 fmt.Println(city)35 fmt.Println(country)36}37import (38func main() {

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import "fmt"2type State struct {3}4func (s *State) Make() {5 fmt.Println("Creating State", s.Name)6}7func main() {8 state := State{Name: "California"}9 state.Make()10}

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 s := state.NewState()4 s.Make()5 fmt.Println("Current state is", s.GetState())6 time.Sleep(2 * time.Second)7 s.Make()8 fmt.Println("Current state is", s.GetState())9 time.Sleep(2 * time.Second)10 s.Make()11 fmt.Println("Current state is", s.GetState())12 time.Sleep(2 * time.Second)13 s.Make()14 fmt.Println("Current state is", s.GetState())15 time.Sleep(2 * time.Second)16 s.Make()17 fmt.Println("Current state is", s.GetState())18 time.Sleep(2 * time.Second)19 s.Make()20 fmt.Println("Current state is", s.GetState())21 time.Sleep(2 * time.Second)22 s.Make()23 fmt.Println("Current state is", s.GetState())24 time.Sleep(2 * time.Second)25}

Full Screen

Full Screen

Make

Using AI Code Generation

copy

Full Screen

1import (2type State struct {3}4func (state *State) Make(numberOfGoRoutines int) {5}6func (state *State) Wait() {7 state.mutex.Lock()8 if state.numberOfGoRoutinesDone == state.numberOfGoRoutines {9 state.mutex.Unlock()10 }11 state.mutex.Unlock()12}13func main() {14 state := new(State)15 state.Make(10)16 for i := 0; i < 10; i++ {17 go func() {18 fmt.Println("Hello")19 state.Wait()20 }()21 }22 state.Wait()23}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful