How to use makeMemMapFs method of lib Package

Best K6 code snippet using lib.makeMemMapFs

archive_test.go

Source:archive_test.go Github

copy

Full Screen

...66 assert.Equal(t, res, NormalizeAndAnonymizePath(res))67 })68 }69}70func makeMemMapFs(t *testing.T, input map[string][]byte) afero.Fs {71 fs := afero.NewMemMapFs()72 for path, data := range input {73 require.NoError(t, afero.WriteFile(fs, path, data, 0o644))74 }75 return fs76}77func getMapKeys(m map[string]afero.Fs) []string {78 keys := make([]string, 0, len(m))79 for key := range m {80 keys = append(keys, key)81 }82 return keys83}84func diffMapFilesystems(t *testing.T, first, second map[string]afero.Fs) bool {85 require.ElementsMatch(t, getMapKeys(first), getMapKeys(second),86 "fs map keys don't match %s, %s", getMapKeys(first), getMapKeys(second))87 for key, fs := range first {88 secondFs := second[key]89 diffFilesystems(t, fs, secondFs)90 }91 return true92}93func diffFilesystems(t *testing.T, first, second afero.Fs) {94 diffFilesystemsDir(t, first, second, "/")95}96func getInfoNames(infos []os.FileInfo) []string {97 names := make([]string, len(infos))98 for i, info := range infos {99 names[i] = info.Name()100 }101 return names102}103func diffFilesystemsDir(t *testing.T, first, second afero.Fs, dirname string) {104 firstInfos, err := afero.ReadDir(first, dirname)105 require.NoError(t, err, dirname)106 secondInfos, err := afero.ReadDir(first, dirname)107 require.NoError(t, err, dirname)108 require.ElementsMatch(t, getInfoNames(firstInfos), getInfoNames(secondInfos), "directory: "+dirname)109 for _, info := range firstInfos {110 path := filepath.Join(dirname, info.Name())111 if info.IsDir() {112 diffFilesystemsDir(t, first, second, path)113 continue114 }115 firstData, err := afero.ReadFile(first, path)116 require.NoError(t, err, path)117 secondData, err := afero.ReadFile(second, path)118 require.NoError(t, err, path)119 assert.Equal(t, firstData, secondData, path)120 }121}122func TestArchiveReadWrite(t *testing.T) {123 t.Parallel()124 t.Run("Roundtrip", func(t *testing.T) {125 t.Parallel()126 arc1 := &Archive{127 Type: "js",128 K6Version: consts.Version,129 Options: Options{130 VUs: null.IntFrom(12345),131 SystemTags: &metrics.DefaultSystemTagSet,132 },133 FilenameURL: &url.URL{Scheme: "file", Path: "/path/to/a.js"},134 Data: []byte(`// a contents`),135 PwdURL: &url.URL{Scheme: "file", Path: "/path/to"},136 Filesystems: map[string]afero.Fs{137 "file": makeMemMapFs(t, map[string][]byte{138 "/path/to/a.js": []byte(`// a contents`),139 "/path/to/b.js": []byte(`// b contents`),140 "/path/to/file1.txt": []byte(`hi!`),141 "/path/to/file2.txt": []byte(`bye!`),142 }),143 "https": makeMemMapFs(t, map[string][]byte{144 "/cdnjs.com/libraries/Faker": []byte(`// faker contents`),145 "/github.com/loadimpact/k6/README.md": []byte(`README`),146 }),147 },148 }149 buf := bytes.NewBuffer(nil)150 require.NoError(t, arc1.Write(buf))151 arc1Filesystems := arc1.Filesystems152 arc1.Filesystems = nil153 arc2, err := ReadArchive(buf)154 require.NoError(t, err)155 arc2Filesystems := arc2.Filesystems156 arc2.Filesystems = nil157 arc2.Filename = ""158 arc2.Pwd = ""159 assert.Equal(t, arc1, arc2)160 diffMapFilesystems(t, arc1Filesystems, arc2Filesystems)161 })162 t.Run("Anonymized", func(t *testing.T) {163 t.Parallel()164 testdata := []struct {165 Pwd, PwdNormAnon string166 }{167 {"/home/myname", "/home/nobody"},168 {filepath.FromSlash("/C:/Users/Administrator"), "/C/Users/nobody"},169 }170 for _, entry := range testdata {171 arc1 := &Archive{172 Type: "js",173 Options: Options{174 VUs: null.IntFrom(12345),175 SystemTags: &metrics.DefaultSystemTagSet,176 },177 FilenameURL: &url.URL{Scheme: "file", Path: fmt.Sprintf("%s/a.js", entry.Pwd)},178 K6Version: consts.Version,179 Data: []byte(`// a contents`),180 PwdURL: &url.URL{Scheme: "file", Path: entry.Pwd},181 Filesystems: map[string]afero.Fs{182 "file": makeMemMapFs(t, map[string][]byte{183 fmt.Sprintf("%s/a.js", entry.Pwd): []byte(`// a contents`),184 fmt.Sprintf("%s/b.js", entry.Pwd): []byte(`// b contents`),185 fmt.Sprintf("%s/file1.txt", entry.Pwd): []byte(`hi!`),186 fmt.Sprintf("%s/file2.txt", entry.Pwd): []byte(`bye!`),187 }),188 "https": makeMemMapFs(t, map[string][]byte{189 "/cdnjs.com/libraries/Faker": []byte(`// faker contents`),190 "/github.com/loadimpact/k6/README.md": []byte(`README`),191 }),192 },193 }194 arc1Anon := &Archive{195 Type: "js",196 Options: Options{197 VUs: null.IntFrom(12345),198 SystemTags: &metrics.DefaultSystemTagSet,199 },200 FilenameURL: &url.URL{Scheme: "file", Path: fmt.Sprintf("%s/a.js", entry.PwdNormAnon)},201 K6Version: consts.Version,202 Data: []byte(`// a contents`),203 PwdURL: &url.URL{Scheme: "file", Path: entry.PwdNormAnon},204 Filesystems: map[string]afero.Fs{205 "file": makeMemMapFs(t, map[string][]byte{206 fmt.Sprintf("%s/a.js", entry.PwdNormAnon): []byte(`// a contents`),207 fmt.Sprintf("%s/b.js", entry.PwdNormAnon): []byte(`// b contents`),208 fmt.Sprintf("%s/file1.txt", entry.PwdNormAnon): []byte(`hi!`),209 fmt.Sprintf("%s/file2.txt", entry.PwdNormAnon): []byte(`bye!`),210 }),211 "https": makeMemMapFs(t, map[string][]byte{212 "/cdnjs.com/libraries/Faker": []byte(`// faker contents`),213 "/github.com/loadimpact/k6/README.md": []byte(`README`),214 }),215 },216 }217 buf := bytes.NewBuffer(nil)218 require.NoError(t, arc1.Write(buf))219 arc1Filesystems := arc1Anon.Filesystems220 arc1Anon.Filesystems = nil221 arc2, err := ReadArchive(buf)222 assert.NoError(t, err)223 arc2.Filename = ""224 arc2.Pwd = ""225 arc2Filesystems := arc2.Filesystems226 arc2.Filesystems = nil227 assert.Equal(t, arc1Anon, arc2)228 diffMapFilesystems(t, arc1Filesystems, arc2Filesystems)229 }230 })231}232func TestArchiveJSONEscape(t *testing.T) {233 t.Parallel()234 arc := &Archive{}235 arc.Filename = "test<.js"236 b, err := arc.json()237 assert.NoError(t, err)238 assert.Contains(t, string(b), "test<.js")239}240func TestUsingCacheFromCacheOnReadFs(t *testing.T) {241 t.Parallel()242 base := afero.NewMemMapFs()243 cached := afero.NewMemMapFs()244 // we specifically have different contents in both places245 require.NoError(t, afero.WriteFile(base, "/wrong", []byte(`ooops`), 0o644))246 require.NoError(t, afero.WriteFile(cached, "/correct", []byte(`test`), 0o644))247 arc := &Archive{248 Type: "js",249 FilenameURL: &url.URL{Scheme: "file", Path: "/correct"},250 K6Version: consts.Version,251 Data: []byte(`test`),252 PwdURL: &url.URL{Scheme: "file", Path: "/"},253 Filesystems: map[string]afero.Fs{254 "file": fsext.NewCacheOnReadFs(base, cached, 0),255 },256 }257 buf := bytes.NewBuffer(nil)258 require.NoError(t, arc.Write(buf))259 newArc, err := ReadArchive(buf)260 require.NoError(t, err)261 data, err := afero.ReadFile(newArc.Filesystems["file"], "/correct")262 require.NoError(t, err)263 require.Equal(t, string(data), "test")264 data, err = afero.ReadFile(newArc.Filesystems["file"], "/wrong")265 require.Error(t, err)266 require.Nil(t, data)267}268func TestArchiveWithDataNotInFS(t *testing.T) {269 t.Parallel()270 arc := &Archive{271 Type: "js",272 FilenameURL: &url.URL{Scheme: "file", Path: "/script"},273 K6Version: consts.Version,274 Data: []byte(`test`),275 PwdURL: &url.URL{Scheme: "file", Path: "/"},276 Filesystems: nil,277 }278 buf := bytes.NewBuffer(nil)279 err := arc.Write(buf)280 require.Error(t, err)281 require.Contains(t, err.Error(), "the main script wasn't present in the cached filesystem")282}283func TestMalformedMetadata(t *testing.T) {284 t.Parallel()285 fs := afero.NewMemMapFs()286 require.NoError(t, afero.WriteFile(fs, "/metadata.json", []byte("{,}"), 0o644))287 b, err := dumpMemMapFsToBuf(fs)288 require.NoError(t, err)289 _, err = ReadArchive(b)290 require.Error(t, err)291 require.Equal(t, err.Error(), `invalid character ',' looking for beginning of object key string`)292}293func TestStrangePaths(t *testing.T) {294 t.Parallel()295 pathsToChange := []string{296 `/path/with spaces/a.js`,297 `/path/with spaces/a.js`,298 `/path/with日本語/b.js`,299 `/path/with spaces and 日本語/file1.txt`,300 }301 for _, pathToChange := range pathsToChange {302 otherMap := make(map[string][]byte, len(pathsToChange))303 for _, other := range pathsToChange {304 otherMap[other] = []byte(`// ` + other + ` contents`)305 }306 arc1 := &Archive{307 Type: "js",308 K6Version: consts.Version,309 Options: Options{310 VUs: null.IntFrom(12345),311 SystemTags: &metrics.DefaultSystemTagSet,312 },313 FilenameURL: &url.URL{Scheme: "file", Path: pathToChange},314 Data: []byte(`// ` + pathToChange + ` contents`),315 PwdURL: &url.URL{Scheme: "file", Path: path.Dir(pathToChange)},316 Filesystems: map[string]afero.Fs{317 "file": makeMemMapFs(t, otherMap),318 },319 }320 buf := bytes.NewBuffer(nil)321 require.NoError(t, arc1.Write(buf), pathToChange)322 arc1Filesystems := arc1.Filesystems323 arc1.Filesystems = nil324 arc2, err := ReadArchive(buf)325 require.NoError(t, err, pathToChange)326 arc2Filesystems := arc2.Filesystems327 arc2.Filesystems = nil328 arc2.Filename = ""329 arc2.Pwd = ""330 assert.Equal(t, arc1, arc2, pathToChange)331 arc1Filesystems["https"] = afero.NewMemMapFs()...

Full Screen

Full Screen

old_archive_test.go

Source:old_archive_test.go Github

copy

Full Screen

...66 for filename, data := range testCases {67 filename, data := filename, data68 t.Run(filename, func(t *testing.T) {69 metadata := `{"filename": "` + filename + `", "options": {}}`70 fs := makeMemMapFs(t, map[string][]byte{71 // files72 "/files/github.com/luckybroman5/http-log-reconstructor/k6/samples/example.js": []byte(`github file`),73 "/files/cdnjs.com/packages/Faker": []byte(`faker file`),74 "/files/example.com/path/to.js": []byte(`example.com file`),75 "/files/_/C/something/path": []byte(`windows file`),76 "/files/_/absolulte/path": []byte(`unix file`),77 // scripts78 "/scripts/github.com/luckybroman5/http-log-reconstructor/k6/samples/example.js2": []byte(`github script`),79 "/scripts/cdnjs.com/packages/Faker2": []byte(`faker script`),80 "/scripts/example.com/path/too.js": []byte(`example.com script`),81 "/scripts/_/C/something/path2": []byte(`windows script`),82 "/scripts/_/absolulte/path2": []byte(`unix script`),83 "/data": []byte(data),84 "/metadata.json": []byte(metadata),85 })86 buf, err := dumpMemMapFsToBuf(fs)87 require.NoError(t, err)88 var (89 expectedFilesystems = map[string]afero.Fs{90 "file": makeMemMapFs(t, map[string][]byte{91 "/C:/something/path": []byte(`windows file`),92 "/absolulte/path": []byte(`unix file`),93 "/C:/something/path2": []byte(`windows script`),94 "/absolulte/path2": []byte(`unix script`),95 }),96 "https": makeMemMapFs(t, map[string][]byte{97 "/example.com/path/to.js": []byte(`example.com file`),98 "/example.com/path/too.js": []byte(`example.com script`),99 "/github.com/luckybroman5/http-log-reconstructor/k6/samples/example.js": []byte(`github file`),100 "/cdnjs.com/packages/Faker": []byte(`faker file`),101 "/github.com/luckybroman5/http-log-reconstructor/k6/samples/example.js2": []byte(`github script`),102 "/cdnjs.com/packages/Faker2": []byte(`faker script`),103 }),104 }105 )106 arc, err := ReadArchive(buf)107 require.NoError(t, err)108 diffMapFilesystems(t, expectedFilesystems, arc.Filesystems)109 })110 }111}112func TestUnknownPrefix(t *testing.T) {113 fs := makeMemMapFs(t, map[string][]byte{114 "/strange/something": []byte(`github file`),115 })116 buf, err := dumpMemMapFsToBuf(fs)117 require.NoError(t, err)118 _, err = ReadArchive(buf)119 require.Error(t, err)120 require.Equal(t, err.Error(),121 "unknown file prefix `strange` for file `strange/something`")122}123func TestFilenamePwdResolve(t *testing.T) {124 var tests = []struct {125 Filename, Pwd, version string126 expectedFilenameURL, expectedPwdURL *url.URL127 expectedError string128 }{129 {130 Filename: "/home/nobody/something.js",131 Pwd: "/home/nobody",132 expectedFilenameURL: &url.URL{Scheme: "file", Path: "/home/nobody/something.js"},133 expectedPwdURL: &url.URL{Scheme: "file", Path: "/home/nobody"},134 },135 {136 Filename: "github.com/luckybroman5/http-log-reconstructor/k6/samples/http2.js",137 Pwd: "github.com/luckybroman5/http-log-reconstructor/k6/samples",138 expectedFilenameURL: &url.URL{Opaque: "github.com/luckybroman5/http-log-reconstructor/k6/samples/http2.js"},139 expectedPwdURL: &url.URL{Opaque: "github.com/luckybroman5/http-log-reconstructor/k6/samples"},140 },141 {142 Filename: "cdnjs.com/libraries/Faker",143 Pwd: "/home/nobody",144 expectedFilenameURL: &url.URL{Opaque: "cdnjs.com/libraries/Faker"},145 expectedPwdURL: &url.URL{Scheme: "file", Path: "/home/nobody"},146 },147 {148 Filename: "example.com/something/dot.js",149 Pwd: "example.com/something/",150 expectedFilenameURL: &url.URL{Host: "example.com", Scheme: "", Path: "/something/dot.js"},151 expectedPwdURL: &url.URL{Host: "example.com", Scheme: "", Path: "/something"},152 },153 {154 Filename: "https://example.com/something/dot.js",155 Pwd: "https://example.com/something",156 expectedFilenameURL: &url.URL{Host: "example.com", Scheme: "https", Path: "/something/dot.js"},157 expectedPwdURL: &url.URL{Host: "example.com", Scheme: "https", Path: "/something"},158 version: "0.25.0",159 },160 {161 Filename: "ftps://example.com/something/dot.js",162 Pwd: "https://example.com/something",163 expectedError: "only supported schemes for imports are file and https",164 version: "0.25.0",165 },166 {167 Filename: "https://example.com/something/dot.js",168 Pwd: "ftps://example.com/something",169 expectedError: "only supported schemes for imports are file and https",170 version: "0.25.0",171 },172 }173 for _, test := range tests {174 metadata := `{175 "filename": "` + test.Filename + `",176 "pwd": "` + test.Pwd + `",177 "k6version": "` + test.version + `",178 "options": {}179 }`180 buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{181 "/metadata.json": []byte(metadata),182 }))183 require.NoError(t, err)184 arc, err := ReadArchive(buf)185 if test.expectedError != "" {186 require.Error(t, err)187 require.Contains(t, err.Error(), test.expectedError)188 } else {189 require.NoError(t, err)190 require.Equal(t, test.expectedFilenameURL, arc.FilenameURL)191 require.Equal(t, test.expectedPwdURL, arc.PwdURL)192 }193 }194}195func TestDerivedExecutionDiscarding(t *testing.T) {196 var emptyConfigMap scheduler.ConfigMap197 var tests = []struct {198 metadata string199 expExecution interface{}200 expError string201 }{202 {203 metadata: `{204 "filename": "/test.js", "pwd": "/",205 "options": { "execution": { "something": "invalid" } }206 }`,207 expExecution: emptyConfigMap,208 },209 {210 metadata: `{211 "filename": "/test.js", "pwd": "/",212 "k6version": "0.24.0",213 "options": { "execution": { "something": "invalid" } }214 }`,215 expExecution: emptyConfigMap,216 },217 {218 metadata: `blah`,219 expError: "invalid character",220 },221 {222 metadata: `{223 "filename": "/test.js", "pwd": "/",224 "k6version": "0.24.0"225 }`,226 expError: "missing options key",227 },228 {229 metadata: `{230 "filename": "/test.js", "pwd": "/",231 "k6version": "0.24.0",232 "options": "something invalid"233 }`,234 expError: "wrong options type in metadata.json",235 },236 {237 metadata: `{238 "filename": "/test.js", "pwd": "/",239 "k6version": "0.25.0",240 "options": { "execution": { "something": "invalid" } }241 }`,242 expError: "cannot unmarshal string",243 },244 {245 metadata: `{246 "filename": "/test.js", "pwd": "/",247 "k6version": "0.25.0",248 "options": { "execution": { "default": { "type": "per-vu-iterations" } } }249 }`,250 expExecution: scheduler.ConfigMap{251 DefaultSchedulerName: scheduler.NewPerVUIterationsConfig(DefaultSchedulerName),252 },253 },254 }255 for _, test := range tests {256 buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{257 "/metadata.json": []byte(test.metadata),258 }))259 require.NoError(t, err)260 arc, err := ReadArchive(buf)261 if test.expError != "" {262 require.Error(t, err)263 require.Contains(t, err.Error(), test.expError)264 } else {265 require.NoError(t, err)266 require.Equal(t, test.expExecution, arc.Options.Execution)267 }268 }269}...

Full Screen

Full Screen

makeMemMapFs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fs := afero.NewMemMapFs()4 fs.Mkdir("/tmp", 0755)5 afero.WriteFile(fs, "/tmp/data", []byte("data"), 0644)6}7import (8func main() {9 fs := afero.makeMemMapFs()10 fs.Mkdir("/tmp", 0755)11 afero.WriteFile(fs, "/tmp/data", []byte("data"), 0644)12}13import (14func main() {15 fs := afero.NewOsFs()16 afero.WriteFile(fs, "file.txt", []byte("data"), 0644)17}18import (19func main() {20 fs := afero.NewMemMapFs()21 afero.WriteFile(fs, "file.txt", []byte("data"), 0644)22}

Full Screen

Full Screen

makeMemMapFs

Using AI Code Generation

copy

Full Screen

1import (2func makeMemMapFs() afero.Fs {3 fs := afero.NewMemMapFs()4 fs.MkdirAll("/root", 0755)5 fs.MkdirAll("/root/dir1", 0755)6 fs.MkdirAll("/root/dir2", 0755)7 fs.MkdirAll("/root/dir3", 0755)8 fs.Create("/root/file1.txt")9 fs.Create("/root/file2.txt")10 fs.Create("/root/file3.txt")11 fs.Create("/root/dir1/file4.txt")12 fs.Create("/root/dir1/file5.txt")13 fs.Create("/root/dir1/file6.txt")14 fs.Create("/root/dir2/file7.txt")15 fs.Create("/root/dir2/file8.txt")16 fs.Create("/root/dir2/file9.txt")17 fs.Create("/root/dir3/file10.txt")18 fs.Create("/root/dir3/file11.txt")19 fs.Create("/root/dir3/file12.txt")20}21import (22func main() {23 fs := makeMemMapFs()24 filepath.Walk("/root", func(path string, info os.FileInfo, err error) error {25 fmt.Println(path)26 })27}28import (29func main() {30 fs := makeMemMapFs()31 filepath.Walk("/root", func(path string, info os.FileInfo, err error) error {32 fmt.Println(path)33 })34}35import (

Full Screen

Full Screen

makeMemMapFs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib := Lib{}4 fs := lib.makeMemMapFs()5 _, _ = fs.Create("test.txt")6 f, _ := fs.Open("test.txt")7 _, _ = f.WriteString("hello world")8 _ = f.Close()9 fileBytes, _ := ioutil.ReadFile("test.txt")10 fmt.Println(string(fileBytes))11}12import "github.com/spf13/afero"13type Lib struct {14}15func (lib *Lib) makeMemMapFs() afero.Fs {16 return afero.NewMemMapFs()17}

Full Screen

Full Screen

makeMemMapFs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 memMapFs := afero.NewMemMapFs()4 file, err := memMapFs.Create("a.txt")5 if err != nil {6 log.Fatal(err)7 }8 defer file.Close()9 _, err = file.WriteString("Hello World!")10 if err != nil {11 log.Fatal(err)12 }13 file, err = memMapFs.Open("a.txt")14 if err != nil {15 log.Fatal(err)16 }17 defer file.Close()18 b := make([]byte, 5)19 _, err = file.Read(b)20 if err != nil {21 log.Fatal(err)22 }23 fmt.Println(string(b))24}25import (26func main() {27 osFs := afero.NewOsFs()28 file, err := osFs.Create("a.txt")29 if err != nil {30 log.Fatal(err)31 }32 defer file.Close()33 _, err = file.WriteString("Hello World!")34 if err != nil {35 log.Fatal(err)36 }37 file, err = osFs.Open("a.txt")38 if err != nil {39 log.Fatal(err)40 }41 defer file.Close()42 b := make([]byte, 5)43 _, err = file.Read(b)44 if err != nil {45 log.Fatal(err)46 }47 fmt.Println(string(b))48}49import (50func main() {51 osFs := afero.NewOsFs()52 file, err := osFs.Create("a.txt")53 if err != nil {

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.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful