How to use Run method of gce Package

Best Syzkaller code snippet using gce.Run

reaper_test.go

Source:reaper_test.go Github

copy

Full Screen

...37)38func init() {39 logger.CreateLogger()40}41type ReaperRunTestCase struct {42 Watchlist []*resources.WatchedResource43 Expected *Reaper44}45var reaperRunTestCases = []ReaperRunTestCase{46 ReaperRunTestCase{47 []*resources.WatchedResource{48 resources.NewWatchedResource(resources.NewResource("TestEarly", "testZone", earlyTime, reaperconfig.ResourceType_GCE_VM), "* 5 * * *"),49 resources.NewWatchedResource(resources.NewResource("TestFuture", "testZone", lateTime, reaperconfig.ResourceType_GCE_VM), "1 * * * *"),50 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "59 * * * *"),51 },52 createTestReaper("testProject", "* * * * *", []*resources.WatchedResource{53 resources.NewWatchedResource(resources.NewResource("TestFuture", "testZone", lateTime, reaperconfig.ResourceType_GCE_VM), "1 * * * *"),54 }...),55 },56 ReaperRunTestCase{57 []*resources.WatchedResource{58 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "1 * * * *"),59 },60 createTestReaper("testProject", "* * * * *", []*resources.WatchedResource{61 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "1 * * * *"),62 }...),63 },64 ReaperRunTestCase{65 []*resources.WatchedResource{66 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo_1", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "59 * * * *"),67 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo_2", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "30 * * * *"),68 },69 createTestReaper("testProject", "* * * * *", []*resources.WatchedResource{70 resources.NewWatchedResource(resources.NewResource("TestTwoMinuteAgo_2", "testZone", twoMinutesAgo, reaperconfig.ResourceType_GCE_VM), "30 * * * *"),71 }...),72 },73}74func TestSweepThroughResources(t *testing.T) {75 server := createServer(deleteComputeEngineResourceHandler)76 defer server.Close()77 testClientOptions := getTestClientOptions(server)78 for _, testCase := range reaperRunTestCases {79 testReaper := createTestReaper("testProject", "* * * * *", testCase.Watchlist...)80 testReaper.FreezeTime(currentTime)81 testReaper.SweepThroughResources(testContext, testClientOptions...)82 if !areWatchlistsEqual(testReaper, testCase.Expected) {83 t.Errorf("Reaper not updated correctly after sweep through watched resources")84 }85 }86}87type UpdateReaperConfigTestCase struct {88 ReaperConfig *reaperconfig.ReaperConfig89 Expected *Reaper90}91var updateReaperConfigTestCases = []UpdateReaperConfigTestCase{92 UpdateReaperConfigTestCase{93 createReaperConfig("SampleProject", "* * * * *"),94 createTestReaper("SampleProject", "* * * * *"),95 },96 UpdateReaperConfigTestCase{97 createReaperConfig("NewProjectID", "* * 10 * *"),98 createTestReaper("NewProjectID", "* * 10 * *"),99 },100 UpdateReaperConfigTestCase{101 createReaperConfig("AnotherProjectID", "59 23 31 12 7"),102 createTestReaper("AnotherProjectID", "59 23 31 12 7"),103 },104 UpdateReaperConfigTestCase{105 createReaperConfig("ProjectIDAgain", "@every 1h30m"),106 createTestReaper("ProjectIDAgain", "@every 1h30m"),107 },108}109func TestUpdateReaperConfig(t *testing.T) {110 testReaper := &Reaper{}111 for _, testCase := range updateReaperConfigTestCases {112 testReaper.UpdateReaperConfig(testCase.ReaperConfig)113 if strings.Compare(testReaper.ProjectID, testCase.Expected.ProjectID) != 0 {114 t.Errorf("Expected project id: %s, got: %s", testCase.Expected.ProjectID, testReaper.ProjectID)115 }116 if !reflect.DeepEqual(testReaper.Schedule, testCase.Expected.Schedule) {117 t.Error("Schedule not updated correctly")118 }119 }120}121type GetResourcesTestCase struct {122 ReaperConfig *reaperconfig.ReaperConfig123 Expected *Reaper124}125var getResourcesTestCases = []GetResourcesTestCase{126 GetResourcesTestCase{127 createReaperConfig(128 "sampleProject", "* * * * *", createResourceConfig(reaperconfig.ResourceType_GCE_VM, "Test", "", "* * * * *", "testZone1"),129 ),130 createTestReaper("sampleProject", "* * * * *", resources.CreateWatchlist(131 []*resources.Resource{132 resources.NewResource("TestName", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),133 resources.NewResource("TestingYetAnotherOne", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),134 },135 "* * * * *",136 )...),137 },138 GetResourcesTestCase{139 createReaperConfig(140 "sampleProject", "* * * * *", createResourceConfig(reaperconfig.ResourceType_GCE_VM, "Test", "Another", "* * * * *", "testZone1"),141 ),142 createTestReaper("sampleProject", "* * * * *", resources.CreateWatchlist(143 []*resources.Resource{144 resources.NewResource("TestName", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),145 },146 "* * * * *",147 )...),148 },149 GetResourcesTestCase{150 createReaperConfig(151 "sampleProject", "* * * * *", createResourceConfig(reaperconfig.ResourceType_GCE_VM, "Test", "", "* * * * *", "testZone1", "testZone2"),152 ),153 createTestReaper("sampleProject", "* * * * *", resources.CreateWatchlist(154 []*resources.Resource{155 resources.NewResource("TestName", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),156 resources.NewResource("TestingYetAnotherOne", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),157 resources.NewResource("TestThis", "testZone2", currentTime, reaperconfig.ResourceType_GCE_VM),158 },159 "* * * * *",160 )...),161 },162 GetResourcesTestCase{163 createReaperConfig(164 "sampleProject", "* * * * *", createResourceConfig(reaperconfig.ResourceType_GCE_VM, "Test", "Testing", "* * * * *", "testZone1", "testZone2"),165 ),166 createTestReaper("sampleProject", "* * * * *", resources.CreateWatchlist(167 []*resources.Resource{168 resources.NewResource("TestName", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),169 resources.NewResource("TestThis", "testZone2", currentTime, reaperconfig.ResourceType_GCE_VM),170 },171 "* * * * *",172 )...),173 },174 GetResourcesTestCase{175 createReaperConfig(176 "sampleProject", "* * * * *", createResourceConfig(reaperconfig.ResourceType_GCE_VM, "Another", "", "* * * * *", "testZone1", "testZone2"),177 ),178 createTestReaper("sampleProject", "* * * * *", resources.CreateWatchlist(179 []*resources.Resource{180 resources.NewResource("AnotherName", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),181 resources.NewResource("TestingYetAnotherOne", "testZone1", currentTime, reaperconfig.ResourceType_GCE_VM),182 resources.NewResource("IsThisAnotherName", "testZone2", currentTime, reaperconfig.ResourceType_GCE_VM),183 },184 "* * * * *",185 )...),186 },187}188func TestGetResources(t *testing.T) {189 server := createServer(getComputeEngineResourcesHandler)190 defer server.Close()191 testClientOptions := getTestClientOptions(server)192 testReaper := &Reaper{}193 setupTestData()194 for _, testCase := range getResourcesTestCases {195 testReaper.config = testCase.ReaperConfig196 testReaper.ProjectID = testCase.ReaperConfig.GetProjectId()197 testReaper.GetResources(testContext, testClientOptions...)198 if !areWatchlistsEqual(testReaper, testCase.Expected) {199 t.Errorf("GetResources did not get correct resources based off config")200 }201 }202}203type RunScheduleTestCase struct {204 Schedule string205 LastRun time.Time206 Expected bool207}208var runScheduleTestCases = []RunScheduleTestCase{209 RunScheduleTestCase{"* * * * *", time.Time{}, true},210 RunScheduleTestCase{"* * * 10 *", time.Time{}, true},211 RunScheduleTestCase{"* 11 * * *", currentTime.Add(-1 * time.Hour), false},212 RunScheduleTestCase{"* 10 * * *", currentTime.Add(-1 * time.Hour), true},213 RunScheduleTestCase{"@every 1m", currentTime.Add(-2 * time.Minute), true},214 RunScheduleTestCase{"@every 1h", currentTime.Add(-1 * time.Hour), true},215}216func TestRunOnSchedule(t *testing.T) {217 for _, testCase := range runScheduleTestCases {218 reaper := createTestReaper("sampleProject", testCase.Schedule)219 reaper.FreezeClock(currentTime)220 reaper.lastRun = testCase.LastRun221 if result := reaper.RunOnSchedule(testContext); result != testCase.Expected {222 t.Errorf("Reaper did run: %v, Should reaper run: %v", result, testCase.Expected)223 }224 }225}226func createServer(handler http.HandlerFunc) *httptest.Server {227 return httptest.NewServer(http.HandlerFunc(handler))228}229func deleteComputeEngineResourceHandler(w http.ResponseWriter, req *http.Request) {230 w.Write([]byte(`{"success": true}`))231}232type GetResourcesResponse struct {233 Items []TestData234}235func getComputeEngineResourcesHandler(w http.ResponseWriter, req *http.Request) {...

Full Screen

Full Screen

cuttlefish.go

Source:cuttlefish.go Github

copy

Full Screen

...75 }76 return inst, nil77}78func (inst *instance) runOnHost(timeout time.Duration, cmd string) error {79 outc, errc, err := inst.gceInst.Run(timeout, nil, cmd)80 if err != nil {81 return fmt.Errorf("failed to run command: %s", err)82 }83 for {84 select {85 case <-vmimpl.Shutdown:86 return nil87 case err := <-errc:88 return err89 case out, ok := <-outc:90 if ok && inst.debug {91 log.Logf(1, "%s", out)92 }93 }94 }95}96func (inst *instance) Copy(hostSrc string) (string, error) {97 gceDst, err := inst.gceInst.Copy(hostSrc)98 if err != nil {99 return "", fmt.Errorf("error copying to worker instance: %s", err)100 }101 deviceDst := filepath.Join(deviceRoot, filepath.Base(hostSrc))102 pushCmd := fmt.Sprintf("adb push %s %s", gceDst, deviceDst)103 if err := inst.runOnHost(5*time.Minute, pushCmd); err != nil {104 return "", fmt.Errorf("error pushing to device: %s", err)105 }106 return deviceDst, nil107}108func (inst *instance) Forward(port int) (string, error) {109 hostForward, err := inst.gceInst.Forward(port)110 if err != nil {111 return "", fmt.Errorf("failed to get IP/port from GCE instance: %s", err)112 }113 cmd := fmt.Sprintf("nohup socat TCP-LISTEN:%d,fork TCP:%s", port, hostForward)114 if err := inst.runOnHost(time.Second, cmd); err != nil && err != vmimpl.ErrTimeout {115 return "", fmt.Errorf("unable to forward port on host: %s", err)116 }117 for i := 0; i < 100; i++ {118 devicePort := vmimpl.RandomPort()119 cmd := fmt.Sprintf("adb reverse tcp:%d tcp:%d", devicePort, port)120 err = inst.runOnHost(10*time.Second, cmd)121 if err == nil {122 return fmt.Sprintf("127.0.0.1:%d", devicePort), nil123 }124 }125 return "", fmt.Errorf("unable to forward port on device: %s", err)126}127func (inst *instance) Close() {128 inst.gceInst.Close()129}130func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (131 <-chan []byte, <-chan error, error) {132 return inst.gceInst.Run(timeout, stop, fmt.Sprintf("adb shell 'cd %s; %s'", deviceRoot, command))133}134func (inst *instance) Diagnose(rep *report.Report) ([]byte, bool) {135 return nil, false136}...

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1gce.Run()2gce.Run()3gce.Run()4gce.Run()5gce.Run()6gce.Run()7gce.Run()8gce.Run()9gce.Run()10gce.Run()11gce.Run()12gce.Run()13gce.Run()14gce.Run()15gce.Run()16gce.Run()17gce.Run()18gce.Run()19gce.Run()20gce.Run()21gce.Run()22gce.Run()23gce.Run()24gce.Run()25gce.Run()26gce.Run()27gce.Run()

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cred, err := ioutil.ReadFile("C:/Users/username/Downloads/credentials.json")4 if err != nil {5 fmt.Println("Error reading credentials file")6 }7 ctx := context.Background()8 client, err := google.DefaultClient(ctx, compute.ComputeScope)9 if err != nil {10 fmt.Println("Error creating client")11 }12 svc, err := compute.New(client)13 if err != nil {14 fmt.Println("Error creating service")15 }16 g := gce{17 }18 g.Run()19}20import (21func main() {22 cred, err := ioutil.ReadFile("C:/Users/username/Downloads/credentials.json")23 if err != nil {24 fmt.Println("Error reading credentials file")25 }26 ctx := context.Background()27 client, err := google.DefaultClient(ctx, compute.ComputeScope)28 if err != nil {29 fmt.Println("Error creating client")30 }31 svc, err := compute.New(client)32 if err != nil {33 fmt.Println("Error creating service")34 }35 g := gce{36 }37 g.Run()38}39import (

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gce := new(GCE)4 gce.Run()5}6import (7func main() {8 gce := new(GCE)9 gce.Run()10}11import (12func main() {13 gce := new(GCE)14 gce.Run()15}16import (17func main() {18 gce := new(GCE)19 gce.Run()20}21import (22func main() {23 gce := new(GCE)24 gce.Run()25}26import (27func main() {28 gce := new(GCE)29 gce.Run()30}31import (32func main() {33 gce := new(GCE)34 gce.Run()35}36import (37func main() {38 gce := new(GCE)39 gce.Run()40}41import (42func main() {43 gce := new(GCE)44 gce.Run()45}46import (47func main() {48 gce := new(GCE)49 gce.Run()50}51import (52func main() {53 gce := new(GCE)54 gce.Run()55}56import (57func main() {58 gce := new(GCE)59 gce.Run()60}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 flag.StringVar(&project, "project", "", "Project ID")4 flag.StringVar(&zone, "zone", "", "Zone name")5 flag.StringVar(&name, "name", "", "Instance name")6 flag.Parse()7 if project == "" || zone == "" || name == "" {8 fmt.Println("Usage: gcloud compute instances create NAME --image IMAGE --zone ZONE --project PROJECT")9 os.Exit(1)10 }11 ctx := context.Background()12 client, err := google.DefaultClient(ctx, compute.ComputeScope)13 if err != nil {14 fmt.Println(err)15 os.Exit(1)16 }17 computeService, err := compute.New(client)18 if err != nil {19 fmt.Println(err)20 os.Exit(1)21 }22 instance := &compute.Instance{23 MachineType: fmt.Sprintf("zones/%s/machineTypes/n1-standard-1", zone),24 Disks: []*compute.AttachedDisk{25 {26 InitializeParams: &compute.AttachedDiskInitializeParams{27 },28 },29 },30 NetworkInterfaces: []*compute.NetworkInterface{31 {32 AccessConfigs: []*compute.AccessConfig{33 {34 },35 },36 },37 },38 }39 op, err := computeService.Instances.Insert(project, zone, instance).Do()40 if err != nil {41 fmt.Println(err)42 os.Exit(1)43 }44 err = waitForOp(computeService, project, zone, op.Name)45 if err != nil {46 fmt.Println(err)47 os.Exit(1)48 }49 fmt.Println("Instance created.")50}51func waitForOp(computeService

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := gce.New()4 g.Run()5 fmt.Println("Done")6}7import (8type GCE struct{}9func New() *GCE {10 return &GCE{}11}12func (g *GCE) Run() {13 fmt.Println("Running")14}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 gce := GCE{}4 gce.Run()5 fmt.Println("In main")6}7import "fmt"8func main() {9 gce := GCE{}10 gce.Run()11 fmt.Println("In main")12}13import "fmt"14func main() {15 gce := GCE{}16 gce.Run()17 fmt.Println("In main")18}19import "fmt"20func main() {21 gce := GCE{}22 gce.Run()23 fmt.Println("In main")24}25import "fmt"26func main() {27 gce := GCE{}28 gce.Run()29 fmt.Println("In main")30}31import "fmt"32func main() {33 gce := GCE{}34 gce.Run()35 fmt.Println("In main")36}37import "fmt"38func main() {39 gce := GCE{}40 gce.Run()41 fmt.Println("In main")42}43import "fmt"44func main() {45 gce := GCE{}46 gce.Run()47 fmt.Println("In main")48}49import "fmt"

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