Best Syzkaller code snippet using gce.CreateInstance
instances_test.go
Source:instances_test.go
...46 err := createInstance(c, nil)47 So(err, ShouldErrLike, "unexpected payload")48 })49 Convey("empty", func() {50 err := createInstance(c, &tasks.CreateInstance{})51 So(err, ShouldErrLike, "ID is required")52 })53 Convey("missing", func() {54 err := createInstance(c, &tasks.CreateInstance{55 Id: "id",56 })57 So(err, ShouldErrLike, "failed to fetch VM")58 })59 })60 Convey("valid", func() {61 Convey("exists", func() {62 datastore.Put(c, &model.VM{63 ID: "id",64 Hostname: "name",65 URL: "url",66 })67 err := createInstance(c, &tasks.CreateInstance{68 Id: "id",69 })70 So(err, ShouldBeNil)71 })72 Convey("drained", func() {73 rt.Handler = func(req interface{}) (int, interface{}) {74 inst, ok := req.(*compute.Instance)75 So(ok, ShouldBeTrue)76 So(inst.Name, ShouldEqual, "name")77 return http.StatusOK, &compute.Operation{}78 }79 rt.Type = reflect.TypeOf(compute.Instance{})80 datastore.Put(c, &model.VM{81 ID: "id",82 Drained: true,83 Hostname: "name",84 })85 err := createInstance(c, &tasks.CreateInstance{86 Id: "id",87 })88 So(err, ShouldBeNil)89 })90 Convey("error", func() {91 Convey("http", func() {92 Convey("transient", func() {93 rt.Handler = func(req interface{}) (int, interface{}) {94 return http.StatusInternalServerError, nil95 }96 rt.Type = reflect.TypeOf(compute.Instance{})97 datastore.Put(c, &model.VM{98 ID: "id",99 Hostname: "name",100 })101 err := createInstance(c, &tasks.CreateInstance{102 Id: "id",103 })104 So(err, ShouldErrLike, "transiently failed to create instance")105 v := &model.VM{106 ID: "id",107 }108 So(datastore.Get(c, v), ShouldBeNil)109 So(v.Hostname, ShouldEqual, "name")110 })111 Convey("permanent", func() {112 rt.Handler = func(req interface{}) (int, interface{}) {113 return http.StatusConflict, nil114 }115 rt.Type = reflect.TypeOf(compute.Instance{})116 datastore.Put(c, &model.VM{117 ID: "id",118 Hostname: "name",119 })120 err := createInstance(c, &tasks.CreateInstance{121 Id: "id",122 })123 So(err, ShouldErrLike, "failed to create instance")124 v := &model.VM{125 ID: "id",126 }127 So(datastore.Get(c, v), ShouldEqual, datastore.ErrNoSuchEntity)128 })129 })130 Convey("operation", func() {131 rt.Handler = func(req interface{}) (int, interface{}) {132 return http.StatusOK, &compute.Operation{133 Error: &compute.OperationError{134 Errors: []*compute.OperationErrorErrors{135 {},136 },137 },138 }139 }140 rt.Type = reflect.TypeOf(compute.Instance{})141 datastore.Put(c, &model.VM{142 ID: "id",143 Hostname: "name",144 })145 err := createInstance(c, &tasks.CreateInstance{146 Id: "id",147 })148 So(err, ShouldErrLike, "failed to create instance")149 v := &model.VM{150 ID: "id",151 }152 So(datastore.Get(c, v), ShouldEqual, datastore.ErrNoSuchEntity)153 })154 })155 Convey("created", func() {156 Convey("pending", func() {157 rt.Handler = func(req interface{}) (int, interface{}) {158 inst, ok := req.(*compute.Instance)159 So(ok, ShouldBeTrue)160 So(inst.Name, ShouldEqual, "name")161 return http.StatusOK, &compute.Operation{}162 }163 rt.Type = reflect.TypeOf(compute.Instance{})164 datastore.Put(c, &model.VM{165 ID: "id",166 Hostname: "name",167 })168 err := createInstance(c, &tasks.CreateInstance{169 Id: "id",170 })171 So(err, ShouldBeNil)172 v := &model.VM{173 ID: "id",174 }175 So(datastore.Get(c, v), ShouldBeNil)176 So(v.Created, ShouldEqual, 0)177 So(v.URL, ShouldBeEmpty)178 })179 Convey("done", func() {180 rt.Handler = func(req interface{}) (int, interface{}) {181 switch rt.Type {182 case reflect.TypeOf(compute.Instance{}):183 // First call, to create the instance.184 inst, ok := req.(*compute.Instance)185 So(ok, ShouldBeTrue)186 So(inst.Name, ShouldEqual, "name")187 rt.Type = reflect.TypeOf(map[string]string{})188 return http.StatusOK, &compute.Operation{189 EndTime: "2018-12-14T15:07:48.200-08:00",190 Status: "DONE",191 TargetLink: "url",192 }193 default:194 // Second call, to check the reason for the conflict.195 // This request should have no body.196 So(*(req.(*map[string]string)), ShouldHaveLength, 0)197 return http.StatusOK, &compute.Instance{198 CreationTimestamp: "2018-12-14T15:07:48.200-08:00",199 NetworkInterfaces: []*compute.NetworkInterface{200 {201 NetworkIP: "0.0.0.1",202 },203 {204 AccessConfigs: []*compute.AccessConfig{205 {206 NatIP: "2.0.0.0",207 },208 },209 NetworkIP: "0.0.0.2",210 },211 {212 AccessConfigs: []*compute.AccessConfig{213 {214 NatIP: "3.0.0.0",215 },216 {217 NatIP: "3.0.0.1",218 },219 },220 NetworkIP: "0.0.0.3",221 },222 },223 SelfLink: "url",224 }225 }226 }227 rt.Type = reflect.TypeOf(compute.Instance{})228 datastore.Put(c, &model.VM{229 ID: "id",230 Hostname: "name",231 })232 err := createInstance(c, &tasks.CreateInstance{233 Id: "id",234 })235 So(err, ShouldBeNil)236 v := &model.VM{237 ID: "id",238 }239 So(datastore.Get(c, v), ShouldBeNil)240 So(v.Created, ShouldNotEqual, 0)241 So(v.NetworkInterfaces, ShouldResemble, []model.NetworkInterface{242 {243 InternalIP: "0.0.0.1",244 },245 {246 ExternalIP: "2.0.0.0",...
gce_client.go
Source:gce_client.go
...14)15// The gceClient interface wraps the Google Compute gceClient interaction.16type gceClient interface {17 Init(context.Context, *jwt.Config) error18 CreateInstance(*host.Host, *GCESettings) (string, error)19 GetInstance(*host.Host) (*compute.Instance, error)20 DeleteInstance(*host.Host) error21}22type gceClientImpl struct {23 InstancesService *compute.InstancesService24}25// Init establishes a connection to a Google Compute endpoint and creates a gceClient that26// can be used to manage instances.27func (c *gceClientImpl) Init(ctx context.Context, config *jwt.Config) error {28 ts := config.TokenSource(ctx)29 client := oauth2.NewClient(ctx, ts)30 grip.Debug("created an OAuth HTTP client to Google services")31 // Connect to Google Compute Engine.32 service, err := compute.New(client)33 if err != nil {34 return errors.Wrap(err, "connecting to Google Compute Engine service")35 }36 grip.Debug("connected to Google Compute Engine service")37 // Get a handle to a specific service for instance configuration.38 c.InstancesService = compute.NewInstancesService(service)39 return nil40}41// CreateInstance requests an instance to be provisioned.42//43// API calls to an instance refer to the instance by the user-provided name (which must be unique)44// and not the ID. If successful, CreateInstance returns the name of the provisioned instance.45func (c *gceClientImpl) CreateInstance(h *host.Host, s *GCESettings) (string, error) {46 // Create instance options to spawn an instance47 machineType := makeMachineType(h.Zone, s.MachineName, s.NumCPUs, s.MemoryMB)48 instance := &compute.Instance{49 Name: h.Id,50 MachineType: machineType,51 Labels: makeLabels(h),52 Tags: &compute.Tags{Items: s.NetworkTags},53 }54 grip.Debug(message.Fields{55 "message": "creating instance",56 "host_id": h.Id,57 "machine_type": machineType,58 })59 // Add the disk with the image URL...
CreateInstance
Using AI Code Generation
1import (2func main() {3 projectID, err := metadata.ProjectID()4 if err != nil {5 log.Fatal(err)6 }7 ts, err := google.DefaultTokenSource(oauth2.NoContext, compute.ComputeScope)8 if err != nil {9 log.Fatal(err)10 }11 ctx := context.Background()12 client := compute.NewClient(ctx, projectID, option.WithTokenSource(ts))13 if err != nil {14 log.Fatal(err)15 }16 i := &compute.Instance{17 Disks: []*compute.AttachedDisk{18 &compute.AttachedDisk{19 InitializeParams: &compute.AttachedDiskInitializeParams{20 },21 },22 },23 NetworkInterfaces: []*compute.NetworkInterface{24 &compute.NetworkInterface{25 AccessConfigs: []*compute.AccessConfig{26 &compute.AccessConfig{27 },28 },29 },30 },31 }32 op, err := client.Instances.Insert(projectID, "us-central1-a", i).Do()33 if err != nil {34 log.Fatal(err)35 }36 _, err = client.GlobalOperations.Wait(ctx, projectID, op.Name).Do()37 if err != nil {38 log.Fatal(err)39 }40 instance, err := client.Instances.Get(projectID, "us-central1
CreateInstance
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 client, err := compute.NewClient(ctx)5 if err != nil {6 fmt.Println("Error creating client: ", err)7 }8 op, err := client.Instances.Create("gce-project-id", "us-central1-a", &compute.Instance{9 Disks: []*compute.AttachedDisk{10 {11 InitializeParams: &compute.AttachedDiskInitializeParams{12 },13 },14 },15 NetworkInterfaces: []*compute.NetworkInterface{16 {17 AccessConfigs: []*compute.AccessConfig{18 {19 },20 },21 },22 },23 }).Do()24 if err != nil {25 fmt.Println("Error creating instance: ", err)26 }27 op, err = client.ZoneOperations.Wait("gce-project-id", "us-central1-a", op.Name).Do()28 if err != nil {29 fmt.Println("Error waiting for operation: ", err)30 }31 if op.Error != nil {32 fmt.Println("Error creating instance: ", op.Error.Errors)33 }34 fmt.Println("Instance created: ", op.TargetLink)35}36import (
CreateInstance
Using AI Code Generation
1import (2func main() {3 client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope)4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 service, err := compute.New(client)9 if err != nil {10 fmt.Println(err)11 os.Exit(1)12 }13 instance := &compute.Instance{14 Disks: []*compute.AttachedDisk{15 {16 InitializeParams: &compute.AttachedDiskInitializeParams{17 },18 },19 },20 NetworkInterfaces: []*compute.NetworkInterface{21 {22 AccessConfigs: []*compute.AccessConfig{23 {24 },25 },26 },27 },28 }29 op, err := service.Instances.Insert("project-id", "us-central1-f", instance).Do()30 if err != nil {31 fmt.Println(err)32 os.Exit(1)33 }34 zoneOpCall := service.ZoneOperations.Get("project-id", op.Zone, op.Name)35 for {36 op, err := zoneOpCall.Do()37 if err != nil {38 fmt.Println(err)39 os.Exit(1)40 }41 if op.Status == "DONE" {42 }43 time.Sleep(1 * time.Second)44 }45 fmt.Printf("Instance created with IP: %v46}
CreateInstance
Using AI Code Generation
1import (2type gce struct {3}4func New(ctx context.Context, opts ...option.ClientOption) (*gce, error) {5 computeService, err := compute.NewService(ctx, opts...)6 if err != nil {7 }8 return &gce{9 }, nil10}11func (g *gce) CreateInstance(ctx context.Context, projectID, zone string, instance *compute.Instance) (*compute.Operation, error) {12 return g.computeService.Instances.Insert(projectID, zone, instance).Context(ctx).Do()13}14func (g *gce) GetInstance(ctx context.Context, projectID, zone, name string) (*compute.Instance, error) {15 return g.computeService.Instances.Get(projectID, zone, name).Context(ctx).Do()16}17func (g *gce) ListInstances(ctx context.Context, projectID, zone string) ([]*compute.Instance, error) {18 err := g.computeService.Instances.List(projectID, zone).Pages(ctx, func(page *compute.InstanceList) error {19 instances = append(instances, page.Items...)20 })21 if err != nil {22 }23}24func (g *gce) DeleteInstance(ctx context.Context, projectID, zone, name string) (*compute.Operation, error) {25 return g.computeService.Instances.Delete(projectID, zone, name).Context(ctx).Do()26}
CreateInstance
Using AI Code Generation
1import (2type Gce struct {3}4func main() {5 http.HandleFunc("/", handle)6 http.ListenAndServe(":8080", nil)7}8func handle(w http.ResponseWriter, r *http.Request) {9 switch r.Method {10 reqBody, _ := ioutil.ReadAll(r.Body)11 json.Unmarshal(reqBody, &gce)12 fmt.Println(gce)13 fmt.Println(gce.InstanceName)14 fmt.Println(gce.ProjectId)15 fmt.Println(gce.Zone)16 fmt.Println(gce.MachineType)17 fmt.Println(gce.Image)18 fmt.Println(gce.DiskSizeGb)19 fmt.Println(gce.RootPassword)20 fmt.Println(gce.Scopes)21 fmt.Println(cmd)22 out, err := exec.Command("bash", "-c", cmd).Output()23 if err != nil {24 log.Fatal(err)25 }26 fmt.Println(string(out))27 w.Write([]byte("Instance Created"))28 w.WriteHeader(http.StatusNotImplemented)29 w.Write([]byte(http.StatusText(http.StatusNotImplemented)))30 }31}
CreateInstance
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 gce.CreateInstance()5}6import (7type Gce struct {8}9func (gce *Gce) CreateInstance() {10 fmt.Println("Hello, playground")11}
CreateInstance
Using AI Code Generation
1import (2type GCE struct {3}4type NetworkInterfaces struct {5}6type AccessConfigs struct {7}8type Disks struct {9}10type InitializeParams struct {11}12func main() {13 gce := GCE{14 NetworkInterfaces: []NetworkInterfaces{15 {16 AccessConfigs: []AccessConfigs{17 {18 },19 },20 },21 },22 Disks: []Disks{23 {24 InitializeParams: InitializeParams{25 },26 },27 },28 }29 jsonData, err := json.Marshal(gce)30 if err != nil {31 fmt.Println(err)32 }
CreateInstance
Using AI Code Generation
1import (2func main() {3 var result interface{}4 var instanceType = reflect.TypeOf(instance)5 var gceType = reflect.TypeOf(gce)6 var instanceValue = reflect.ValueOf(instance)7 var gceValue = reflect.ValueOf(gce)8 var method = gceType.MethodByName("CreateInstance")9 args = append(args, instanceValue)10 result = method.Func.Call(args)11 if len(result) > 0 {12 err, _ = result[0].Interface().(error)13 }14 if err != nil {15 fmt.Println(err)16 }17}18import (19type Instance struct {20}21type Gce struct {22}23type IInstance interface {24 CreateInstance(instance Instance) error25}26func (gce Gce) CreateInstance(instance Instance) error {27 fmt.Println("Creating instance: " + instance.Name)28}29import (30func main() {31 var result interface{}32 var instanceType = reflect.TypeOf(instance)33 var gceType = reflect.TypeOf(gce)34 var instanceValue = reflect.ValueOf(instance)35 var gceValue = reflect.ValueOf(gce)36 var method = gceType.MethodByName("CreateInstance")37 args = append(args, instanceValue)38 result = method.Func.Call(args)39 if len(result) > 0 {40 err, _ = result[0].Interface().(error)41 }42 if err != nil {43 fmt.Println(err)44 }45}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!