github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/tests/function_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/taubyte/go-project-schema/project"
     8  	"github.com/taubyte/tau-cli/constants"
     9  	functionPrompts "github.com/taubyte/tau-cli/prompts/function"
    10  )
    11  
    12  // Define a method to test your monkey
    13  func TestFunctionAll(t *testing.T) {
    14  	runTests(t, createFunctionMonkey(), true)
    15  }
    16  
    17  func createFunctionMonkey() *testSpider {
    18  
    19  	// Define shared variables
    20  	command := "function"
    21  	profileName := "test"
    22  	projectName := "test_project"
    23  	testName := "test_function"
    24  
    25  	testDomain := "test_domain_1"
    26  	testDomainFqdn := "hal.computers.com"
    27  
    28  	testService := "test_service_1"
    29  	testServiceProtocol := "/test/v1"
    30  	network := "Test"
    31  
    32  	// Create a basic resource of name using a template
    33  	basicNewTemplate := func(name, template string) []string {
    34  		return []string{
    35  			"new", "-y", command,
    36  			"--name", name,
    37  			"--description", "some function description",
    38  			"--tags", "tag1, tag2,   tag3",
    39  			"--timeout", "10s",
    40  			"--memory", "10",
    41  			"--memory-unit", "GB",
    42  			"--type", "http",
    43  			"--use-template",
    44  			"--lang", "go",
    45  			"--template", template,
    46  			"--domains", "test_domain_1",
    47  			"--method", "get",
    48  			"--paths", "/",
    49  			"--source", ".",
    50  			"--call", "ping",
    51  		}
    52  	}
    53  
    54  	basicNewNoTemplate := func(name string) []string {
    55  		return []string{
    56  			"new", "-y", command,
    57  			"--name", name,
    58  			"--description", "some function description",
    59  			"--tags", "tag1, tag2,   tag3",
    60  			"--timeout", "10s",
    61  			"--memory", "10",
    62  			"--memory-unit", "GB",
    63  			"--type", "http",
    64  			"--no-use-template",
    65  			"--domains", "test_domain_1",
    66  			"--method", "get",
    67  			"--paths", "/",
    68  			"--source", ".",
    69  			"--call", "ping",
    70  		}
    71  	}
    72  
    73  	testLibrary := "test_library"
    74  
    75  	// The config that will be written
    76  	getConfigString := basicGetConfigString(profileName, projectName)
    77  
    78  	// Run before each test
    79  	beforeEach := func(tt testMonkey) [][]string {
    80  		tt.env[constants.CurrentProjectEnvVarName] = projectName
    81  		tt.env[constants.CurrentSelectedNetworkName] = network
    82  		return nil
    83  	}
    84  
    85  	// Define test monkeys that will run in parallel
    86  	tests := []testMonkey{
    87  		{
    88  			name: "Query New basic with template",
    89  			args: []string{"query", command, testName},
    90  			wantOut: []string{
    91  				"tag1", "tag2", "tag3", testName, testDomain, "GET",
    92  				"Paths       │ /",
    93  				"Source      │ inline",
    94  				"Call        │ ping",
    95  				"test_domain_1",
    96  			},
    97  			mock: true,
    98  			preRun: [][]string{
    99  				basicNewDomain(testDomain, testDomainFqdn),
   100  			},
   101  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   102  			wantDir: []string{
   103  				"test_project/code/functions/test_function/ping_pong.go",
   104  			},
   105  			children: []testMonkey{
   106  				{
   107  					args:    basicNewTemplate(testName, "ping_pong"),
   108  					wantOut: []string{command, testName, "Created"},
   109  				},
   110  			},
   111  		},
   112  		{
   113  			name: "Query New basic with template and application",
   114  			mock: true,
   115  			args: []string{"query", command, testName},
   116  			wantOut: []string{
   117  				"tag1", "tag2", "tag3", testName, testDomain, "GET",
   118  				"Paths       │ /",
   119  				"Source      │ inline",
   120  				"Call        │ ping",
   121  				"test_domain_1",
   122  			},
   123  			preRun: [][]string{
   124  				basicNewDomain(testDomain, testDomainFqdn),
   125  			},
   126  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   127  			wantDir: []string{
   128  				"test_project/code/applications/test_app/functions/test_function/ping_pong.go",
   129  			},
   130  			env: map[string]string{
   131  				constants.CurrentApplicationEnvVarName: "test_app",
   132  			},
   133  			children: []testMonkey{
   134  				{
   135  					args:    basicNewTemplate(testName, "ping_pong"),
   136  					wantOut: []string{command, testName, "Created"},
   137  				},
   138  			},
   139  		},
   140  		{
   141  			mock: true,
   142  			name: "Query Edit basic with template",
   143  			args: []string{"query", command, testName},
   144  			wantOut: []string{
   145  				"tag4", testName, "test_domain_2", "GET",
   146  				"Paths       │ /, /test",
   147  				"Source      │ libraries/test_library",
   148  				"Call        │ test_library.ping",
   149  				"10m",
   150  				"50MB",
   151  			},
   152  			wantDir: []string{
   153  				"test_project/code/functions/test_function/ping_pong.go",
   154  			},
   155  			children: []testMonkey{
   156  				{
   157  					args:    basicNewTemplate(testName, "ping_pong"),
   158  					wantOut: []string{command, testName, "Created"},
   159  				},
   160  				{
   161  					args: []string{
   162  						"edit", "-y", command, "--name", testName,
   163  						"--description", "some function description",
   164  						"--tags", "tag4",
   165  						"--timeout", "10m",
   166  						"--memory", "50",
   167  						"--memory-unit", "MB",
   168  						"--type", "http",
   169  						"--domains", "test_domain_2",
   170  						"--method", "get",
   171  						"--paths", "/,/test",
   172  						"--source", "test_library",
   173  						"--call", "test_library.ping",
   174  					},
   175  					wantOut: []string{command, testName, "Edited"},
   176  				},
   177  			},
   178  			preRun: [][]string{
   179  				basicNewLibrary(testLibrary),
   180  				basicNewDomain(testDomain, testDomainFqdn),
   181  				basicNewDomain("test_domain_2", testDomainFqdn),
   182  			},
   183  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   184  		},
   185  		{
   186  			name: "Query New basic no template",
   187  			mock: true,
   188  			args: []string{"query", command, testName},
   189  			wantOut: []string{
   190  				"tag1", "tag2", "tag3", testName, testDomain, "GET",
   191  				"Paths       │ /",
   192  				"Source      │ inline",
   193  				"Call        │ ping",
   194  				"test_domain_1",
   195  			},
   196  			preRun: [][]string{
   197  				basicNewDomain(testDomain, testDomainFqdn),
   198  			},
   199  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   200  			wantDir: []string{
   201  				"test_project/code/functions/test_function/test_function.md",
   202  			},
   203  			children: []testMonkey{
   204  				{
   205  					args:    basicNewNoTemplate(testName),
   206  					wantOut: []string{command, testName, "Created"},
   207  				},
   208  			},
   209  		},
   210  		{
   211  			name: "Query Edit basic no template",
   212  			mock: true,
   213  			args: []string{"query", command, testName},
   214  			wantOut: []string{
   215  				"tag4", testName, "test_domain_2", "GET",
   216  				"Paths       │ /, /test",
   217  				"Source      │ libraries/test_library",
   218  				"Call        │ test_library.ping",
   219  				"10m",
   220  				"50MB",
   221  			},
   222  			wantDir: []string{
   223  				"test_project/code/functions/test_function/test_function.md",
   224  			},
   225  			preRun: [][]string{
   226  				basicNewDomain(testDomain, testDomainFqdn),
   227  				basicNewDomain("test_domain_2", testDomainFqdn),
   228  				basicNewLibrary(testLibrary),
   229  			},
   230  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   231  			children: []testMonkey{
   232  				{
   233  					args:    basicNewNoTemplate(testName),
   234  					wantOut: []string{command, testName, "Created"},
   235  				},
   236  				{
   237  					args: []string{
   238  						"edit", "-y", command, "--name", testName,
   239  						"--description", "some function description",
   240  						"--tags", "tag4",
   241  						"--timeout", "10m",
   242  						"--memory", "50",
   243  						"--memory-unit", "MB",
   244  						"--type", "http",
   245  						"--domains", "test_domain_2",
   246  						"--method", "get",
   247  						"--paths", "/,/test",
   248  						"--source", "test_library",
   249  						"--call", "test_library.ping",
   250  					},
   251  					wantOut: []string{command, testName, "Edited"},
   252  				},
   253  			},
   254  		},
   255  		{
   256  			name:     "Query delete",
   257  			mock:     true,
   258  			args:     []string{"query", command, testName},
   259  			exitCode: 1,
   260  			errOut:   []string{fmt.Sprintf(functionPrompts.NotFound, testName)},
   261  			preRun: [][]string{
   262  				basicNewDomain(testDomain, testDomainFqdn),
   263  				basicNewTemplate(testName, "ping_pong"),
   264  			},
   265  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   266  			children: []testMonkey{
   267  				{
   268  					name:    "Delete basic",
   269  					args:    []string{"delete", "-y", command, "--name", testName},
   270  					wantOut: []string{command, testName, "Deleted"},
   271  				},
   272  			},
   273  		},
   274  		{
   275  			name: "Query list",
   276  			mock: true,
   277  			args: []string{"query", command, "--list"},
   278  			wantOut: []string{
   279  				testName + "1",
   280  				testName + "2",
   281  				// testName+"3", deleted
   282  				testName + "4",
   283  				testName + "5",
   284  			},
   285  			dontWantOut: []string{
   286  				testName + "3",
   287  			},
   288  			preRun: [][]string{
   289  				basicNewDomain(testDomain, testDomainFqdn),
   290  				basicNewTemplate(testName+"1", "ping_pong"),
   291  				basicNewTemplate(testName+"2", "ping_pong"),
   292  				basicNewTemplate(testName+"3", "ping_pong"),
   293  				{"delete", "-y", command, "--name", testName + "3"},
   294  				basicNewTemplate(testName+"4", "ping_pong"),
   295  				basicNewTemplate(testName+"5", "ping_pong"),
   296  			},
   297  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   298  		},
   299  		{
   300  			name: "new p2p query",
   301  			args: []string{"query", command, testName},
   302  			wantOut: []string{
   303  				"tag1", "tag2", "tag3", testName,
   304  				"Command     │ doPing",
   305  				"Source      │ inline",
   306  				"Call        │ ping",
   307  				"Protocol    │ test_service_1",
   308  			},
   309  			wantDir: []string{
   310  				"test_project/code/functions/test_function/test_function.md",
   311  			},
   312  			preRun: [][]string{
   313  				basicNewService(testService, testServiceProtocol),
   314  			},
   315  			children: []testMonkey{
   316  				{
   317  					args: []string{
   318  						"new", "-y", command, "--name", testName,
   319  						"--description", "some function description",
   320  						"--tags", "tag1,tag2,tag3",
   321  						"--timeout", "10m",
   322  						"--memory", "50",
   323  						"--memory-unit", "MB",
   324  						"--type", "p2p",
   325  						"--no-use-template",
   326  						"--command", "doPing",
   327  						"--no-local",
   328  						"--protocol", testServiceProtocol,
   329  						"--source", "inline",
   330  						"--call", "ping",
   331  					},
   332  					wantOut: []string{command, testName, "Created"},
   333  				},
   334  			},
   335  		},
   336  		{
   337  			name: "new pubsub query",
   338  			args: []string{"query", command, testName},
   339  			wantOut: []string{
   340  				"tag1", "tag2", "tag3", testName,
   341  				"Channel     │ doPing",
   342  				"Source      │ inline",
   343  				"Call        │ ping",
   344  			},
   345  			wantDir: []string{
   346  				"test_project/code/functions/test_function/test_function.md",
   347  			},
   348  			children: []testMonkey{
   349  				{
   350  					args: []string{
   351  						"new", "-y", command, "--name", testName,
   352  						"--description", "some function description",
   353  						"--tags", "tag1,tag2,tag3",
   354  						"--timeout", "10m",
   355  						"--memory", "50",
   356  						"--memory-unit", "MB",
   357  						"--type", "pubsub",
   358  						"--no-use-template",
   359  						"--channel", "doPing",
   360  						"--no-local",
   361  						"--paths", "/",
   362  						"--source", "inline",
   363  						"--call", "ping",
   364  					},
   365  					wantOut: []string{command, testName, "Created"},
   366  				},
   367  			},
   368  		},
   369  		{
   370  			name: "edit http to p2p, query, confirm yaml",
   371  			args: []string{"query", command, testName},
   372  			wantOut: []string{
   373  				"tag1", "tag2", "tag3", testName,
   374  				"Command     │ doPing",
   375  				"Source      │ inline",
   376  				"Call        │ ping",
   377  			},
   378  			mock: true,
   379  			preRun: [][]string{
   380  				basicNewDomain(testDomain, testDomainFqdn),
   381  				basicNewService(testService, testServiceProtocol),
   382  				basicNewTemplate(testName, "ping_pong"),
   383  			},
   384  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   385  			confirmProject: func(p project.Project) error {
   386  				function, err := p.Function(testName, "")
   387  				if err != nil {
   388  					return nil
   389  				}
   390  
   391  				getter := function.Get()
   392  				return ConfirmEmpty(getter.Method(), getter.Domains(), getter.Paths())
   393  			},
   394  			children: []testMonkey{
   395  				{
   396  					name: "edit http to p2p",
   397  					args: []string{
   398  						"edit", "-y", command, "--name", testName,
   399  						"--description", "some function description",
   400  						"--tags", "tag1,tag2,tag3",
   401  						"--timeout", "10m",
   402  						"--memory", "50",
   403  						"--memory-unit", "MB",
   404  						"--type", "p2p",
   405  						"--command", "doPing",
   406  						"--no-local",
   407  						"--protocol", testServiceProtocol,
   408  						"--source", "inline",
   409  						"--call", "ping",
   410  					},
   411  					wantOut: []string{command, testName, "Edited"},
   412  				},
   413  			},
   414  		},
   415  		{
   416  			name: "edit p2p to http, query, confirm yaml",
   417  			args: []string{"query", command, testName},
   418  			confirmProject: func(p project.Project) error {
   419  				function, err := p.Function(testName, "")
   420  				if err != nil {
   421  					return nil
   422  				}
   423  
   424  				getter := function.Get()
   425  				return ConfirmEmpty(getter.Command(), getter.Local(), getter.Protocol())
   426  			},
   427  			mock: true,
   428  			preRun: [][]string{
   429  				basicNewService(testService, testServiceProtocol),
   430  				basicNewDomain("test_domain_2", testDomainFqdn),
   431  				basicNewLibrary(testLibrary),
   432  			},
   433  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   434  			wantOut: []string{
   435  				"test_function",
   436  				"some function description",
   437  				"tag4",
   438  				"http",
   439  				"test_domain_2",
   440  				"/test",
   441  				"GET",
   442  				"libraries/test_library",
   443  				"test_library.ping",
   444  			},
   445  			children: []testMonkey{
   446  				{
   447  					name: "new p2p",
   448  					args: []string{
   449  						"new", "-y", command, "--name", testName,
   450  						"--description", "some function description",
   451  						"--tags", "tag1,tag2,tag3",
   452  						"--timeout", "10m",
   453  						"--memory", "50",
   454  						"--memory-unit", "MB",
   455  						"--type", "p2p",
   456  						"--no-use-template",
   457  						"--command", "doPing",
   458  						"--local",
   459  						"--protocol", testServiceProtocol,
   460  						"--source", "inline",
   461  						"--call", "ping",
   462  					},
   463  					wantOut: []string{command, testName, "Created"},
   464  				},
   465  				{
   466  					name: "edit p2p to http",
   467  					args: []string{
   468  						"edit", "-y", command, "--name", testName,
   469  						"--description", "some function description",
   470  						"--tags", "tag4",
   471  						"--timeout", "10m",
   472  						"--memory", "50",
   473  						"--memory-unit", "MB",
   474  						"--type", "http",
   475  						"--domains", "test_domain_2",
   476  						"--method", "get",
   477  						"--paths", "/,/test",
   478  						"--source", "test_library",
   479  						"--call", "test_library.ping",
   480  					},
   481  					wantOut: []string{command, testName, "Edited"},
   482  				},
   483  			},
   484  		},
   485  		{
   486  			name: "edit pubsub to p2p, query, confirm yaml",
   487  			args: []string{"query", command, testName},
   488  			confirmProject: func(p project.Project) error {
   489  				function, err := p.Function(testName, "")
   490  				if err != nil {
   491  					return nil
   492  				}
   493  
   494  				getter := function.Get()
   495  				return ConfirmEmpty(getter.Channel(), getter.Domains(), getter.Method(), getter.Paths())
   496  			},
   497  			preRun: [][]string{
   498  				basicNewService(testService, testServiceProtocol),
   499  			},
   500  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   501  			wantOut: []string{
   502  				"test_function",
   503  				"some function description",
   504  				"tag4",
   505  				"p2p",
   506  				"doPing",
   507  				"true",
   508  				"Protocol    │ test_service_1",
   509  				"test_library",
   510  				"test_library.ping",
   511  			},
   512  			children: []testMonkey{
   513  				{
   514  					name: "new pubsub",
   515  					args: []string{
   516  						"new", "-y", command, "--name", testName,
   517  						"--description", "some function description",
   518  						"--tags", "tag1,tag2,tag3",
   519  						"--timeout", "10m",
   520  						"--memory", "50",
   521  						"--memory-unit", "MB",
   522  						"--type", "pubsub",
   523  						"--no-use-template",
   524  						"--channel", "doPing",
   525  						"--no-local",
   526  						"--source", "inline",
   527  						"--call", "ping",
   528  					},
   529  					wantOut: []string{command, testName, "Created"},
   530  				},
   531  				{
   532  					name: "edit pubsub to p2p",
   533  					args: []string{
   534  						"edit", "-y", command, "--name", testName,
   535  						"--description", "some function description",
   536  						"--tags", "tag4",
   537  						"--timeout", "10m",
   538  						"--memory", "50",
   539  						"--memory-unit", "MB",
   540  						"--type", "p2p",
   541  						"--command", "doPing",
   542  						"--local",
   543  						"--protocol", testServiceProtocol,
   544  						"--source", "test_library",
   545  						"--call", "test_library.ping",
   546  					},
   547  					wantOut: []string{command, testName, "Edited"},
   548  				},
   549  			},
   550  		},
   551  	}
   552  	return &testSpider{projectName, tests, beforeEach, getConfigString, "function"}
   553  }