Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Agones GameServer integration test #580

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion agones/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,38 @@ use std::{
};

use k8s_openapi::{
api::core::v1::Namespace, apimachinery::pkg::apis::meta::v1::ObjectMeta, chrono,
api::{
core::v1::{
Container, Namespace, PodSpec, PodTemplateSpec, ResourceRequirements, ServiceAccount,
},
rbac::v1::{RoleBinding, RoleRef, Subject},
},
apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::ObjectMeta},
chrono,
};
use kube::{
api::{DeleteParams, ListParams, PostParams},
runtime::wait::Condition,
Api, ResourceExt,
};
use tokio::sync::OnceCell;

use quilkin::config::watch::agones::crd::{
GameServer, GameServerPort, GameServerSpec, GameServerState,
};

mod pod;
mod sidecar;

#[allow(dead_code)]
static CLIENT: OnceCell<Client> = OnceCell::const_new();
#[allow(dead_code)]
const IMAGE_TAG: &str = "IMAGE_TAG";
const DELETE_DELAY_SECONDS: &str = "DELETE_DELAY_SECONDS";
/// A simple udp server that returns packets that are sent to it.
/// See: <https://github.com/googleforgames/agones/tree/main/examples/simple-game-server>
/// for more details.
pub const GAMESERVER_IMAGE: &str = "gcr.io/agones-images/simple-game-server:0.13";

pub struct Client {
/// The Kubernetes client
Expand Down Expand Up @@ -123,5 +140,103 @@ async fn setup_namespace(client: kube::Client) -> String {
.await
.expect("namespace to be created");

add_agones_service_account(client, test_namespace.name()).await;

test_namespace.name()
}

async fn add_agones_service_account(client: kube::Client, namespace: String) {
let service_accounts: Api<ServiceAccount> = Api::namespaced(client.clone(), namespace.as_str());
let role_bindings: Api<RoleBinding> = Api::namespaced(client, namespace.as_str());
let pp = PostParams::default();
let labels = BTreeMap::from([("app".to_string(), "agones".to_string())]);

let service_account = ServiceAccount {
metadata: ObjectMeta {
name: Some("agones-sdk".into()),
namespace: Some(namespace.clone()),
labels: Some(labels.clone()),
..Default::default()
},
..Default::default()
};

let service_account = service_accounts
.create(&pp, &service_account)
.await
.unwrap();

let role_binding = RoleBinding {
metadata: ObjectMeta {
name: Some("agones-sdk-access".into()),
namespace: Some(namespace.clone()),
labels: Some(labels),
..Default::default()
},
role_ref: RoleRef {
api_group: "rbac.authorization.k8s.io".into(),
kind: "ClusterRole".into(),
name: "agones-sdk".into(),
},
subjects: Some(vec![Subject {
kind: "ServiceAccount".into(),
name: service_account.name(),
namespace: Some(namespace),
api_group: None,
}]),
};

let _ = role_bindings.create(&pp, &role_binding).await.unwrap();
}

/// Returns a test GameServer with the UDP test binary that is used for
/// Agones e2e tests.
pub fn game_server() -> GameServer {
let mut resources = BTreeMap::new();

resources.insert("cpu".into(), Quantity("30m".into()));
resources.insert("memory".into(), Quantity("32Mi".into()));

GameServer {
metadata: ObjectMeta {
generate_name: Some("gameserver-".into()),
..Default::default()
},
spec: GameServerSpec {
ports: vec![GameServerPort {
container_port: 7654,
host_port: None,
name: "udp-port".into(),
port_policy: Default::default(),
container: None,
protocol: Default::default(),
}],
template: PodTemplateSpec {
spec: Some(PodSpec {
containers: vec![Container {
name: "game-server".into(),
image: Some(GAMESERVER_IMAGE.into()),
resources: Some(ResourceRequirements {
limits: Some(resources.clone()),
requests: Some(resources),
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
},
..Default::default()
},
status: None,
}
}

/// Condition to wait for a GameServer to become Ready.
pub fn is_gameserver_ready() -> impl Condition<GameServer> {
|obj: Option<&GameServer>| {
obj.and_then(|gs| gs.status.clone())
.map(|status| matches!(status.state, GameServerState::Ready))
.unwrap_or(false)
}
}
65 changes: 65 additions & 0 deletions agones/src/sidecar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#[cfg(test)]
mod tests {
use crate::{game_server, is_gameserver_ready, Client};
use kube::api::PostParams;
use kube::runtime::wait::await_condition;
use kube::{Api, ResourceExt};
use quilkin::config::watch::agones::crd::GameServer;
use quilkin::test_utils::TestHelper;
use std::time::Duration;
use tokio::time::timeout;

#[tokio::test]
/// This test exists to test that we can connect to an Agones GameServer directly.
/// Useful in case one is not sure if an issue is Quilkin or general connectivity issues, such
/// as a firewall settings, or an issue with Agones itself.
async fn gameserver_no_sidecar() {
let client = Client::new().await;
let gameservers: Api<GameServer> =
Api::namespaced(client.kubernetes.clone(), client.namespace.as_str());

let gs = game_server();

let pp = PostParams::default();
let gs = gameservers.create(&pp, &gs).await.unwrap();

let name = gs.name();
let ready = await_condition(gameservers.clone(), name.as_str(), is_gameserver_ready());
timeout(Duration::from_secs(30), ready)
.await
.expect("GameServer should be ready")
.unwrap();
let gs = gameservers.get(name.as_str()).await.unwrap();

let t = TestHelper::default();
let recv = t.open_socket_and_recv_single_packet().await;
let status = gs.status.unwrap();
let address = format!("{}:{}", status.address, status.ports.unwrap()[0].port);
recv.socket
.send_to("hello".as_bytes(), address)
.await
.unwrap();

let response = timeout(Duration::from_secs(30), recv.packet_rx)
.await
.expect("should receive packet")
.unwrap();
assert_eq!("ACK: hello\n", response);
}
}
2 changes: 1 addition & 1 deletion src/config/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

mod agones;
pub mod agones;
mod fs;

pub use self::{agones::watch as agones, fs::watch as fs};
18 changes: 17 additions & 1 deletion src/config/watch/agones.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
mod crd;
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub mod crd;

use futures::TryStreamExt;
use k8s_openapi::api::core::v1::ConfigMap;
Expand Down
Loading