agones.dev/agones@v1.54.0/test/sdk/restapi/http-api-test.go (about)

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"golang.org/x/net/context"
    25  
    26  	alpha "agones.dev/agones/test/sdk/restapi/alpha/swagger"
    27  	beta "agones.dev/agones/test/sdk/restapi/beta/swagger"
    28  	"agones.dev/agones/test/sdk/restapi/swagger"
    29  )
    30  
    31  func main() {
    32  	log.Println("Client is starting")
    33  	conf := swagger.NewConfiguration()
    34  	portStr := os.Getenv("AGONES_SDK_HTTP_PORT")
    35  	conf.BasePath = "http://localhost:" + portStr
    36  	cli := swagger.NewAPIClient(conf)
    37  
    38  	log.Println("Alpha Client is starting")
    39  	alphaConf := alpha.NewConfiguration()
    40  	alphaConf.BasePath = "http://localhost:" + portStr
    41  	alphaCli := alpha.NewAPIClient(alphaConf)
    42  
    43  	log.Println("Beta Client is starting")
    44  	betaConf := beta.NewConfiguration()
    45  	betaConf.BasePath = "http://localhost:" + portStr
    46  	betaCli := beta.NewAPIClient(betaConf)
    47  
    48  	ctx := context.Background()
    49  
    50  	// Wait for SDK server to start the test (15 seconds)
    51  	for c := 0; c < 15; c++ {
    52  		_, _, err := cli.SDKApi.Ready(ctx, swagger.SdkEmpty{})
    53  		if err == nil {
    54  			break
    55  		} else {
    56  			log.Printf("Could not send Ready: %v\n", err)
    57  		}
    58  		time.Sleep(1 * time.Second)
    59  	}
    60  
    61  	c := make(chan string)
    62  
    63  	once := true
    64  	go func() {
    65  		for {
    66  			gs, _, err := cli.SDKApi.WatchGameServer(ctx)
    67  			log.Printf("Watch response: %+v", gs)
    68  			if err != nil {
    69  				log.Printf("Error in WatchGameServer: %v\n", err)
    70  				return
    71  			} else {
    72  				if gs.Result.ObjectMeta != nil {
    73  					uid := gs.Result.ObjectMeta.Uid
    74  					if once {
    75  						c <- uid
    76  						once = false
    77  					}
    78  				} else {
    79  					log.Printf("Could not read GS Uid \n")
    80  				}
    81  			}
    82  		}
    83  	}()
    84  
    85  	_, _, err := cli.SDKApi.Health(ctx, swagger.SdkEmpty{})
    86  	if err != nil {
    87  		log.Fatalf("Could not send health check: %v\n", err)
    88  	}
    89  
    90  	_, _, err = cli.SDKApi.Reserve(ctx, swagger.SdkDuration{"5"})
    91  	if err != nil {
    92  		log.Fatalf("Could not send Reserve: %v\n", err)
    93  	}
    94  
    95  	_, _, err = cli.SDKApi.Allocate(ctx, swagger.SdkEmpty{})
    96  	if err != nil {
    97  		log.Fatalf("Could not send Allocate: %v\n", err)
    98  	}
    99  
   100  	gs, _, err := cli.SDKApi.GetGameServer(ctx)
   101  	if err != nil {
   102  		log.Fatalf("Could not GetGameserver: %v\n", err)
   103  	}
   104  
   105  	creationTS := gs.ObjectMeta.CreationTimestamp
   106  
   107  	_, _, err = cli.SDKApi.SetLabel(ctx, swagger.SdkKeyValue{"creationTimestamp", creationTS})
   108  	if err != nil {
   109  		log.Fatalf("Could not SetLabel: %v\n", err)
   110  	}
   111  
   112  	// TODO: fix WatchGameServer() HTTP API Swagger definition and remove the following lines
   113  	go func() {
   114  		c <- gs.ObjectMeta.Uid
   115  	}()
   116  
   117  	uid := <-c
   118  	_, _, err = cli.SDKApi.SetAnnotation(ctx, swagger.SdkKeyValue{"UID", uid})
   119  	if err != nil {
   120  		log.Fatalf("Could not SetAnnotation: %v\n", err)
   121  	}
   122  
   123  	// easy feature flag check
   124  	if strings.Contains(os.Getenv("FEATURE_GATES"), "PlayerTracking=true") {
   125  		testPlayers(ctx, alphaCli)
   126  	} else {
   127  		log.Print("Player Tracking not enabled, skipping.")
   128  	}
   129  
   130  	if strings.Contains(os.Getenv("FEATURE_GATES"), "CountsAndLists=true") {
   131  		testCounters(ctx, betaCli)
   132  		testLists(ctx, betaCli)
   133  	} else {
   134  		log.Print("Counts and Lists not enabled, skipping.")
   135  	}
   136  
   137  	_, _, err = cli.SDKApi.Shutdown(ctx, swagger.SdkEmpty{})
   138  	if err != nil {
   139  		log.Fatalf("Could not GetGameserver: %v\n", err)
   140  	}
   141  	log.Println("REST API test finished, all queries were performed")
   142  }
   143  
   144  func testPlayers(ctx context.Context, alphaCli *alpha.APIClient) {
   145  	capacity := "10"
   146  	if _, _, err := alphaCli.SDKApi.SetPlayerCapacity(ctx, alpha.AlphaCount{Count: capacity}); err != nil {
   147  		log.Fatalf("Could not set Capacity: %v\n", err)
   148  	}
   149  
   150  	count, _, err := alphaCli.SDKApi.GetPlayerCapacity(ctx)
   151  	if err != nil {
   152  		log.Fatalf("Could not get Capacity: %v\n", err)
   153  	}
   154  	if count.Count != capacity {
   155  		log.Fatalf("Player Capacity should be %s, but is %s", capacity, count.Count)
   156  	}
   157  
   158  	playerID := "1234"
   159  	if ok, _, err := alphaCli.SDKApi.PlayerConnect(ctx, alpha.AlphaPlayerId{PlayerID: playerID}); err != nil {
   160  		log.Fatalf("Error registering player as connected: %s", err)
   161  	} else if !ok.Bool_ {
   162  		log.Fatalf("PlayerConnect returned false")
   163  	}
   164  
   165  	if ok, _, err := alphaCli.SDKApi.IsPlayerConnected(ctx, playerID); err != nil {
   166  		log.Fatalf("Error checking if player is connected: %s", err)
   167  	} else if !ok.Bool_ {
   168  		log.Fatalf("IsPlayerConnected returned false")
   169  	}
   170  
   171  	if list, _, err := alphaCli.SDKApi.GetConnectedPlayers(ctx); err != nil {
   172  		log.Fatalf("Error getting connected player: %s", err)
   173  	} else if len(list.List) == 0 {
   174  		log.Fatalf("No connected players returned")
   175  	}
   176  
   177  	if ok, _, err := alphaCli.SDKApi.PlayerDisconnect(ctx, alpha.AlphaPlayerId{PlayerID: playerID}); err != nil {
   178  		log.Fatalf("Error registering player as disconnected: %s", err)
   179  	} else if !ok.Bool_ {
   180  		log.Fatalf("PlayerDisconnect returned false")
   181  	}
   182  
   183  	if count, _, err := alphaCli.SDKApi.GetPlayerCount(ctx); err != nil {
   184  		log.Fatalf("Error retrieving player count: %s", err)
   185  	} else if count.Count != "0" {
   186  		log.Fatalf("Player Count should be 0, but is %v", count)
   187  	}
   188  }
   189  
   190  func testCounters(ctx context.Context, betaCli *beta.APIClient) {
   191  	// Tests are expected to run sequentially on the same pre-defined Counter in the localsdk server
   192  	counterName := "rooms"
   193  
   194  	expectedCounter := beta.BetaCounter{Name: counterName, Count: "1", Capacity: "10"}
   195  	if counter, _, err := betaCli.SDKApi.GetCounter(ctx, counterName); err != nil {
   196  		log.Fatalf("Error getting Counter: %s", err)
   197  	} else {
   198  		if !cmp.Equal(expectedCounter, counter) {
   199  			log.Fatalf("GetCounter expected Counter: %v, got Counter: %v", expectedCounter, counter)
   200  		}
   201  	}
   202  
   203  	// Test updatecounter, setcapacitycounter
   204  	expectedCounter = beta.BetaCounter{Name: counterName, Count: "0", Capacity: "42"}
   205  	if counter, _, err := betaCli.SDKApi.UpdateCounter(ctx, beta.TheRequestedUpdateToMakeToTheCounter{CountDiff: "-1", Capacity: "42"}, counterName); err != nil {
   206  		log.Fatalf("Error getting Counter: %s", err)
   207  	} else {
   208  		if !cmp.Equal(expectedCounter, counter) {
   209  			log.Fatalf("UpdateCounter expected Counter: %v, got Counter: %v", expectedCounter, counter)
   210  		}
   211  	}
   212  
   213  	// Test setcountcounter
   214  	expectedCounter = beta.BetaCounter{Name: counterName, Count: "40", Capacity: "42"}
   215  	if counter, _, err := betaCli.SDKApi.UpdateCounter(ctx, beta.TheRequestedUpdateToMakeToTheCounter{Count: "40", Capacity: "42"}, counterName); err != nil {
   216  		log.Fatalf("Error getting Counter: %s", err)
   217  	} else {
   218  		if !cmp.Equal(expectedCounter, counter) {
   219  			log.Fatalf("UpdateCounter expected Counter: %v, got Counter: %v", expectedCounter, counter)
   220  		}
   221  	}
   222  }
   223  
   224  func testLists(ctx context.Context, betaCli *beta.APIClient) {
   225  	// Tests are expected to run sequentially on the same pre-defined List in the localsdk server
   226  	listName := "players"
   227  
   228  	expectedList := beta.BetaList{Name: listName, Values: []string{"test0", "test1", "test2"}, Capacity: "100"}
   229  	if list, _, err := betaCli.SDKApi.GetList(ctx, listName); err != nil {
   230  		log.Fatalf("Error getting List: %s", err)
   231  	} else {
   232  		if !cmp.Equal(expectedList, list) {
   233  			log.Fatalf("GetList expected List: %v, got List: %v", expectedList, list)
   234  		}
   235  	}
   236  
   237  	expectedList = beta.BetaList{Name: listName, Values: []string{"test123", "test456"}, Capacity: "10"}
   238  	if list, _, err := betaCli.SDKApi.UpdateList(ctx, beta.TheListToUpdate{Values: []string{"test123", "test456"}, Capacity: "10"}, listName); err != nil {
   239  		log.Fatalf("Error getting List: %s", err)
   240  	} else {
   241  		if !cmp.Equal(expectedList, list) {
   242  			log.Fatalf("UpdateList expected List: %v, got List: %v", expectedList, list)
   243  		}
   244  	}
   245  
   246  	expectedList = beta.BetaList{Name: listName, Values: []string{"test123", "test456", "test789"}, Capacity: "10"}
   247  	if list, _, err := betaCli.SDKApi.AddListValue(ctx, beta.SdkAddListValueBody{Value: "test789"}, listName); err != nil {
   248  		log.Fatalf("Error getting List: %s", err)
   249  	} else {
   250  		if !cmp.Equal(expectedList, list) {
   251  			log.Fatalf("AddListValue expected List: %v, got List: %v", expectedList, list)
   252  		}
   253  	}
   254  
   255  	expectedList = beta.BetaList{Name: listName, Values: []string{"test123", "test789"}, Capacity: "10"}
   256  	if list, _, err := betaCli.SDKApi.RemoveListValue(ctx, beta.SdkRemoveListValueBody{Value: "test456"}, listName); err != nil {
   257  		log.Fatalf("Error getting List: %s", err)
   258  	} else {
   259  		if !cmp.Equal(expectedList, list) {
   260  			log.Fatalf("RemoveListValue expected List: %v, got List: %v", expectedList, list)
   261  		}
   262  	}
   263  }