Best K6 code snippet using lib.Apply
testsuite.go
Source:testsuite.go
...19)20// TestApplier applies the test context21type TestApplier interface {22 TestContext(context.Context) (context.Context, func(), error)23 Apply(context.Context, Applier) (string, func(), error)24}25// FSSuite runs the path test suite26func FSSuite(t *testing.T, a TestApplier) {27 t.Run("Basic", makeTest(t, a, basicTest))28 t.Run("Deletion", makeTest(t, a, deletionTest))29 t.Run("Update", makeTest(t, a, updateTest))30 t.Run("DirectoryPermission", makeTest(t, a, directoryPermissionsTest))31 t.Run("ParentDirectoryPermission", makeTest(t, a, parentDirectoryPermissionsTest))32 t.Run("HardlinkUnmodified", makeTest(t, a, hardlinkUnmodified))33 t.Run("HardlinkBeforeUnmodified", makeTest(t, a, hardlinkBeforeUnmodified))34 t.Run("HardlinkBeforeModified", makeTest(t, a, hardlinkBeforeModified))35}36func makeTest(t *testing.T, ta TestApplier, as []Applier) func(t *testing.T) {37 return func(t *testing.T) {38 ctx, cleanup, err := ta.TestContext(context.Background())39 if err != nil {40 t.Fatalf("Unable to get test context: %+v", err)41 }42 defer cleanup()43 applyDir, err := ioutil.TempDir("", "test-expected-")44 if err != nil {45 t.Fatalf("Unable to make temp directory: %+v", err)46 }47 defer os.RemoveAll(applyDir)48 for i, a := range as {49 testDir, c, err := ta.Apply(ctx, a)50 if err != nil {51 t.Fatalf("Apply failed at %d: %+v", i, err)52 }53 if err := a.Apply(applyDir); err != nil {54 if c != nil {55 c()56 }57 t.Fatalf("Error applying change to apply directory: %+v", err)58 }59 err = CheckDirectoryEqual(applyDir, testDir)60 if c != nil {61 c()62 }63 if err != nil {64 t.Fatalf("Directories not equal at %d (expected <> tested): %+v", i, err)65 }66 }67 }68}69var (70 // baseApplier creates a basic filesystem layout71 // with multiple types of files for basic tests.72 baseApplier = Apply(73 CreateDir("/etc/", 0755),74 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost"), 0644),75 Link("/etc/hosts", "/etc/hosts.allow"),76 CreateDir("/usr/local/lib", 0755),77 CreateFile("/usr/local/lib/libnothing.so", []byte{0x00, 0x00}, 0755),78 Symlink("libnothing.so", "/usr/local/lib/libnothing.so.2"),79 CreateDir("/home", 0755),80 CreateDir("/home/derek", 0700),81 // TODO: CreateSocket: how should Sockets be handled in continuity?82 )83 // basicTest covers basic operations84 basicTest = []Applier{85 baseApplier,86 Apply(87 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost.localdomain"), 0644),88 CreateFile("/etc/fstab", []byte("/dev/sda1\t/\text4\tdefaults 1 1\n"), 0600),89 CreateFile("/etc/badfile", []byte(""), 0666),90 CreateFile("/home/derek/.zshrc", []byte("#ZSH is just better\n"), 0640),91 ),92 Apply(93 Remove("/etc/badfile"),94 Rename("/home/derek", "/home/notderek"),95 ),96 Apply(97 RemoveAll("/usr"),98 Remove("/etc/hosts.allow"),99 ),100 Apply(101 RemoveAll("/home"),102 CreateDir("/home/derek", 0700),103 CreateFile("/home/derek/.bashrc", []byte("#not going away\n"), 0640),104 Link("/etc/hosts", "/etc/hosts.allow"),105 ),106 }107 // deletionTest covers various deletion scenarios to ensure108 // deletions are properly picked up and applied109 deletionTest = []Applier{110 Apply(111 CreateDir("/test/somedir", 0755),112 CreateDir("/lib", 0700),113 CreateFile("/lib/hidden", []byte{}, 0644),114 ),115 Apply(116 CreateFile("/test/a", []byte{}, 0644),117 CreateFile("/test/b", []byte{}, 0644),118 CreateDir("/test/otherdir", 0755),119 CreateFile("/test/otherdir/.empty", []byte{}, 0644),120 RemoveAll("/lib"),121 CreateDir("/lib", 0700),122 CreateFile("/lib/not-hidden", []byte{}, 0644),123 ),124 Apply(125 Remove("/test/a"),126 Remove("/test/b"),127 RemoveAll("/test/otherdir"),128 CreateFile("/lib/newfile", []byte{}, 0644),129 ),130 }131 // updateTest covers file updates for content and permission132 updateTest = []Applier{133 Apply(134 CreateDir("/d1", 0755),135 CreateDir("/d2", 0700),136 CreateFile("/d1/f1", []byte("something..."), 0644),137 CreateFile("/d1/f2", []byte("else..."), 0644),138 CreateFile("/d1/f3", []byte("entirely..."), 0644),139 ),140 Apply(141 CreateFile("/d1/f1", []byte("file content of a different length"), 0664),142 Remove("/d1/f3"),143 CreateFile("/d1/f3", []byte("updated content"), 0664),144 Chmod("/d1/f2", 0766),145 Chmod("/d2", 0777),146 ),147 }148 // directoryPermissionsTest covers directory permissions on update149 directoryPermissionsTest = []Applier{150 Apply(151 CreateDir("/d1", 0700),152 CreateDir("/d2", 0751),153 CreateDir("/d3", 0777),154 ),155 Apply(156 CreateFile("/d1/f", []byte("irrelevant"), 0644),157 CreateDir("/d1/d", 0700),158 CreateFile("/d1/d/f", []byte("irrelevant"), 0644),159 CreateFile("/d2/f", []byte("irrelevant"), 0644),160 CreateFile("/d3/f", []byte("irrelevant"), 0644),161 ),162 }163 // parentDirectoryPermissionsTest covers directory permissions for updated164 // files165 parentDirectoryPermissionsTest = []Applier{166 Apply(167 CreateDir("/d1", 0700),168 CreateDir("/d1/a", 0700),169 CreateDir("/d1/a/b", 0700),170 CreateDir("/d1/a/b/c", 0700),171 CreateFile("/d1/a/b/f", []byte("content1"), 0644),172 CreateDir("/d2", 0751),173 CreateDir("/d2/a/b", 0751),174 CreateDir("/d2/a/b/c", 0751),175 CreateFile("/d2/a/b/f", []byte("content1"), 0644),176 ),177 Apply(178 CreateFile("/d1/a/b/f", []byte("content1"), 0644),179 Chmod("/d1/a/b/c", 0700),180 CreateFile("/d2/a/b/f", []byte("content2"), 0644),181 Chmod("/d2/a/b/c", 0751),182 ),183 }184 hardlinkUnmodified = []Applier{185 baseApplier,186 Apply(187 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost.localdomain"), 0644),188 ),189 Apply(190 Link("/etc/hosts", "/etc/hosts.deny"),191 ),192 }193 // Hardlink name before with modification194 // Tests link is created for unmodified files when a new hard linked file is seen first195 hardlinkBeforeUnmodified = []Applier{196 baseApplier,197 Apply(198 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost.localdomain"), 0644),199 ),200 Apply(201 Link("/etc/hosts", "/etc/before-hosts"),202 ),203 }204 // Hardlink name after without modification205 // tests link is created for modified file with new hardlink206 hardlinkBeforeModified = []Applier{207 baseApplier,208 Apply(209 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost.localdomain"), 0644),210 ),211 Apply(212 Remove("/etc/hosts"),213 CreateFile("/etc/hosts", []byte("127.0.0.1 localhost"), 0644),214 Link("/etc/hosts", "/etc/before-hosts"),215 ),216 }217)...
init.go
Source:init.go
1package cmd2import (3 "github.com/aaabramov/goal/lib"4 "github.com/spf13/cobra"5 "gopkg.in/yaml.v2"6 "io/ioutil"7)8// initCmd represents the init command9var initCmd = &cobra.Command{10 Use: "init",11 Short: "Create new goal.yaml file in current file",12 Long: "Create new goal.yaml file in current file",13 Run: func(cmd *cobra.Command, args []string) {14 initGoals("goal.yaml")15 },16}17func init() {18 rootCmd.AddCommand(initCmd)19 // TODO: support templates20}21var defaultGoals = map[string]lib.YamlGoal{22 "test": {23 Desc: "Run go tests",24 Cmd: "go",25 Args: []string{"test", "-v", "./..."},26 Assert: nil,27 },28 "terraform-workspace": {29 Desc: "Current terraform workspace",30 Cmd: "terraform",31 Args: []string{"workspace", "show"},32 Assert: nil,33 },34 "terraform-apply": {35 Desc: "See https://github.com/aaabramov/goal/tree/master/examples/terraform",36 Envs: &map[string]lib.YamlEnvGoal{37 "dev": {38 Desc: "Terraform apply on dev",39 Cmd: "terraform",40 Args: []string{"apply", "-var-file", "vars/dev.tfvars"},41 Assert: []lib.YamlAssert{42 {43 TerraformWorkspace: "dev",44 }},45 },46 "stage": {47 Desc: "Terraform apply on stage",48 Cmd: "terraform",49 Args: []string{"apply", "-var-file", "vars/stage.tfvars"},50 Assert: []lib.YamlAssert{51 {52 TerraformWorkspace: "stage",53 },54 },55 },56 },57 },58 "k8s-apply": {59 Desc: "See https://github.com/aaabramov/goal/tree/master/examples/kubectl",60 Envs: &map[string]lib.YamlEnvGoal{61 "dev": {62 Desc: "kubectl apply on dev",63 Cmd: "kubectl",64 Args: []string{"apply", "-f", "deployment.yaml"},65 Assert: []lib.YamlAssert{66 {67 KubectlContext: "gke_project_region_dev",68 },69 {70 Approval: "yes",71 },72 },73 },74 "stage": {75 Desc: "kubectl apply on stage",76 Cmd: "kubectl",77 Args: []string{"apply", "-f", "deployment.yaml"},78 Assert: []lib.YamlAssert{79 {80 KubectlContext: "gke_project_region_stage",81 },82 {83 Approval: "yes",84 },85 },86 },87 },88 },89 "helm-upgrade": {90 Desc: "See https://github.com/aaabramov/goal/tree/master/examples/helm",91 Envs: &map[string]lib.YamlEnvGoal{92 "dev": {93 Desc: "helm upgrade on dev",94 Cmd: "helm",95 Args: []string{"upgrade", "release-name", "-f", "values.yaml", "-f", "values/dev.yaml", "."},96 Assert: []lib.YamlAssert{97 {98 KubectlContext: "gke_project_region_dev",99 }},100 },101 "stage": {102 Desc: "helm upgrade on stage",103 Cmd: "helm",104 Args: []string{"upgrade", "release-name", "-f", "values.yaml", "-f", "values/stage.yaml", "."},105 Assert: []lib.YamlAssert{106 {107 KubectlContext: "gke_project_region_stage",108 }},109 },110 },111 },112 "gcloud-ssh": {113 Desc: "See https://github.com/aaabramov/goal/tree/master/examples/gcloud",114 Envs: &map[string]lib.YamlEnvGoal{115 "dev": {116 Desc: "SSH to dev",117 Cmd: "gcloud",118 Args: []string{"compute", "ssh", "dev-vm", "--zone=us-central1-c"},119 Assert: []lib.YamlAssert{120 {121 GcloudProject: "dev-project",122 }},123 },124 "stage": {125 Desc: "SSH to stage",126 Cmd: "gcloud",127 Args: []string{"compute", "ssh", "stage-vm", "--zone=us-central1-c"},128 Assert: []lib.YamlAssert{129 {130 GcloudProject: "stage-project",131 }},132 },133 },134 },135}136func initGoals(filename string) {137 lib.Info("â Generating default %s file", filename)138 bytes, err := yaml.Marshal(defaultGoals)139 if err != nil {140 lib.Fatal("â Failed to generate default YAML for goals")141 }142 if err = ioutil.WriteFile(filename, bytes, 0644); err != nil {143 lib.Fatal("â Failed to create %s", filename)144 }145 lib.Info("â
Generated default %s file. Try running `goal` to see available goals.", filename)146}...
Apply
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(lib.Apply(5, 10))5}6func Apply(a, b int) int {7}
Apply
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 lib.Apply()5}6import (7func Apply() {8 fmt.Println("Applying...")9}
Apply
Using AI Code Generation
1import (2func main() {3 z = lib.Apply(x, y)4 fmt.Printf("z = %d", z)5}6import (7func main() {8 z = lib.Apply(x, y)9 fmt.Printf("z = %d", z)10}11import (12func main() {13 z = lib.Apply(x, y)14 fmt.Printf("z = %d", z)15}
Apply
Using AI Code Generation
1import (2func main() {3 fmt.Println(lib.Apply(1, 2, lib.Add))4}5import (6func main() {7 fmt.Println(lib.Apply(1, 2, lib.Multiply))8}9func Add(a, b int) int {10}11func Multiply(a, b int) int {12}13func Apply(a, b int, f func(int, int) int) int {14 return f(a, b)15}16import "testing"17func TestAdd(t *testing.T) {18 if Add(1, 2) != 3 {19 t.Fail()20 }21}22func TestMultiply(t *testing.T) {23 if Multiply(2, 3) != 6 {24 t.Fail()25 }26}
Apply
Using AI Code Generation
1import (2func main() {3 a.Apply()4 fmt.Println("Done")5}6import (7func main() {8 a.Apply()9 fmt.Println("Done")10 b.Apply()11 fmt.Println("Done")12}
Apply
Using AI Code Generation
1func main() {2 fmt.Println(a, b)3 lib.Apply(a, b)4}5import "fmt"6func Apply(a int, b int) {7 fmt.Println(a, b)8}9import "testing"10func TestApply(t *testing.T) {11 Apply(1, 2)12}13--- PASS: TestApply (0.00s)
Apply
Using AI Code Generation
1import (2func main() {3 fmt.Println(lib.Apply(a, b))4}5func Apply(a int, b int) int {6}7import (
Apply
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 apply.Apply(1, 2, add)5}6func add(x int, y int) int {7}8func Apply(x int, y int, f func(int, int) int) int {9 return f(x, y)10}11import (12func main() {13 fmt.Println("Hello World")14 apply.Apply(1, 2, add)15}16func add(x int, y int) int {17}18func Apply(x int, y int, f func(int, int) int) int {19 return f(x, y)20}
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!!