-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
ENVTEST_K8S_VERSION = 1.27.1 | ||
|
||
.PHONY: test | ||
test-integration: envtest | ||
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -v | ||
|
||
## Location to install dependencies to | ||
LOCALBIN ?= $(shell pwd)/bin | ||
$(LOCALBIN): | ||
mkdir -p $(LOCALBIN) | ||
ENVTEST ?= $(LOCALBIN)/setup-envtest | ||
|
||
.PHONY: envtest | ||
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. | ||
$(ENVTEST): $(LOCALBIN) | ||
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
disv1 "k8s.io/api/discovery/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
func TestIntegration(t *testing.T) { | ||
const modelName = "test-model-a" | ||
deploy := testDeployment(modelName) | ||
|
||
require.NoError(t, testK8sClient.Create(testCtx, deploy)) | ||
|
||
backendRequests := &atomic.Int32{} | ||
testBackend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
backendRequests.Add(1) | ||
w.WriteHeader(200) | ||
})) | ||
testBackendURL, err := url.Parse(testBackend.URL) | ||
require.NoError(t, err) | ||
testBackendPort, err := strconv.Atoi(testBackendURL.Port()) | ||
require.NoError(t, testK8sClient.Create(testCtx, endpointSlice(modelName, testBackendURL.Hostname(), int32(testBackendPort)))) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
time.Sleep(3 * time.Second) | ||
|
||
body := []byte(fmt.Sprintf(`{"model": %q}`, modelName)) | ||
req, err := http.NewRequest(http.MethodPost, testServer.URL, bytes.NewReader(body)) | ||
requireNoError(err) | ||
|
||
res, err := testHTTPClient.Do(req) | ||
require.NoError(t, err) | ||
require.Equal(t, 200, res.StatusCode) | ||
require.Equal(t, int32(1), backendRequests.Load()) | ||
}() | ||
|
||
require.EventuallyWithT(t, func(t *assert.CollectT) { | ||
err := testK8sClient.Get(testCtx, types.NamespacedName{Namespace: deploy.Namespace, Name: deploy.Name}, deploy) | ||
assert.NoError(t, err, "getting the deployment") | ||
assert.GreaterOrEqual(t, deploy.Spec.Replicas, 1, "scale-up should have occurred") | ||
}, 3*time.Second, time.Second/2, "waiting for the deployment to be scaled up") | ||
|
||
t.Logf("Waiting for wait group") | ||
wg.Wait() | ||
} | ||
|
||
func testDeployment(name string) *appsv1.Deployment { | ||
return &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name + "-deploy", | ||
Namespace: testNamespace, | ||
Labels: map[string]string{ | ||
"app": name, | ||
}, | ||
Annotations: map[string]string{ | ||
"lingo.substratus.ai/models": name, | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app": name, | ||
}, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": name, | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "model", | ||
Image: "some-model:1.2.3", | ||
Ports: []corev1.ContainerPort{ | ||
{ | ||
ContainerPort: 80, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func endpointSlice(name, ip string, port int32) *disv1.EndpointSlice { | ||
return &disv1.EndpointSlice{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name + "-deploy", | ||
Namespace: testNamespace, | ||
Labels: map[string]string{ | ||
disv1.LabelServiceName: name + "-deploy", | ||
}, | ||
}, | ||
AddressType: disv1.AddressTypeIPv4, | ||
Endpoints: []disv1.Endpoint{ | ||
{ | ||
Addresses: []string{ip}, | ||
Conditions: disv1.EndpointConditions{ | ||
Ready: ptr.To(true), | ||
Serving: ptr.To(true), | ||
Terminating: ptr.To(false), | ||
}, | ||
}, | ||
}, | ||
Ports: []disv1.EndpointPort{ | ||
{ | ||
Name: ptr.To("http"), | ||
Port: ptr.To(port), | ||
Protocol: ptr.To(corev1.ProtocolTCP), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
) | ||
|
||
var ( | ||
testNamespace = "test" | ||
testK8sClient client.Client | ||
testEnv *envtest.Environment | ||
testCtx context.Context | ||
testCancel context.CancelFunc | ||
testServer *httptest.Server | ||
testHTTPClient = &http.Client{Timeout: 10 * time.Second} | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
testCtx, testCancel = context.WithCancel(context.TODO()) | ||
|
||
log.Println("bootstrapping test environment") | ||
testEnv = &envtest.Environment{} | ||
cfg, err := testEnv.Start() | ||
requireNoError(err) | ||
|
||
requireNoError(clientgoscheme.AddToScheme(scheme)) | ||
|
||
testK8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) | ||
requireNoError(err) | ||
|
||
requireNoError(testK8sClient.Create(testCtx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, | ||
})) | ||
|
||
mgr, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
Scheme: scheme, | ||
Metrics: metricsserver.Options{ | ||
BindAddress: "0", | ||
}, | ||
}) | ||
requireNoError(err) | ||
|
||
fifo := NewFIFOQueueManager(1, 1000) | ||
|
||
endpoints, err := NewEndpointsManager(mgr) | ||
requireNoError(err) | ||
endpoints.EndpointSizeCallback = fifo.UpdateQueueSize | ||
|
||
scaler, err := NewDeploymentManager(mgr) | ||
requireNoError(err) | ||
scaler.Namespace = testNamespace | ||
scaler.ScaleDownPeriod = 30 * time.Second | ||
|
||
autoscaler := NewAutoscaler() | ||
autoscaler.Interval = 3 * time.Second | ||
autoscaler.AverageCount = 10 // 10 * 3 seconds = 30 sec avg | ||
autoscaler.Scaler = scaler | ||
autoscaler.FIFO = fifo | ||
go autoscaler.Start() | ||
|
||
handler := &Handler{ | ||
Deployments: scaler, | ||
Endpoints: endpoints, | ||
FIFO: fifo, | ||
} | ||
testServer = httptest.NewServer(handler) | ||
defer testServer.Close() | ||
|
||
ctx := ctrl.SetupSignalHandler() | ||
|
||
go func() { | ||
log.Println("starting manager") | ||
requireNoError(mgr.Start(ctx)) | ||
}() | ||
|
||
log.Println("running tests") | ||
code := m.Run() | ||
|
||
// TODO: Run cleanup on ctrl-C, etc. | ||
log.Println("stopping manager") | ||
testCancel() | ||
log.Println("stopping test environment") | ||
requireNoError(testEnv.Stop()) | ||
|
||
os.Exit(code) | ||
} | ||
|
||
func requireNoError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |