-
Notifications
You must be signed in to change notification settings - Fork 5
Add CRD detection guard and unit tests for Agones views #12
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
Open
NAME-ASHWANIYADAV
wants to merge
4
commits into
agones-dev:main
Choose a base branch
from
NAME-ASHWANIYADAV:feat/crd-detection-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c47bbc0
plugin: Add CRD detection guard for Agones sidebar views
NAME-ASHWANIYADAV ac453fd
plugin: Add unit tests for CRD detection guard
NAME-ASHWANIYADAV b33c7f0
fix: make Agones installation check robust by validating APIResourceList
NAME-ASHWANIYADAV a09a9a1
plugin: Move isAgonesInstalled into hook and add TSDoc
NAME-ASHWANIYADAV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,77 @@ | ||
| /* | ||
| * Copyright Contributors to Agones a Series of LF Projects, 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| import Box from '@mui/material/Box'; | ||
| import CircularProgress from '@mui/material/CircularProgress'; | ||
| import Grid from '@mui/material/Grid'; | ||
| import Link from '@mui/material/Link'; | ||
| import Typography from '@mui/material/Typography'; | ||
| import React from 'react'; | ||
| import { useAgonesInstalled } from '../hooks/useAgonesInstalled'; | ||
|
|
||
| interface NotInstalledBannerProps { | ||
| isLoading?: boolean; | ||
| } | ||
|
|
||
| function NotInstalledBanner({ isLoading = false }: NotInstalledBannerProps) { | ||
| if (isLoading) { | ||
| return ( | ||
| <Box display="flex" justifyContent="center" alignItems="center" p={2} minHeight="200px"> | ||
| <CircularProgress /> | ||
| </Box> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Box display="flex" justifyContent="center" alignItems="center" p={2} minHeight="200px"> | ||
| <Grid container spacing={2} direction="column" justifyContent="center" alignItems="center"> | ||
| <Grid item> | ||
| <Typography variant="h5"> | ||
| Agones was not detected on your cluster. If you haven't already, please install it. | ||
| </Typography> | ||
| </Grid> | ||
| <Grid item> | ||
| <Typography> | ||
| Learn how to{' '} | ||
| <Link | ||
| href="https://agones.dev/site/docs/installation/install-agones/" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| install | ||
| </Link>{' '} | ||
| Agones | ||
| </Typography> | ||
| </Grid> | ||
| </Grid> | ||
| </Box> | ||
| ); | ||
| } | ||
|
|
||
| interface AgonesInstallCheckProps { | ||
| children: React.ReactNode; | ||
| fallback?: React.ReactNode; | ||
| } | ||
|
|
||
| export function AgonesInstallCheck({ children, fallback }: AgonesInstallCheckProps) { | ||
| const { isAgonesInstalled, isAgonesCheckLoading } = useAgonesInstalled(); | ||
|
|
||
| if (!isAgonesInstalled) { | ||
| return <>{fallback || <NotInstalledBanner isLoading={isAgonesCheckLoading} />}</>; | ||
| } | ||
|
|
||
| return <>{children}</>; | ||
| } |
This file contains hidden or 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,71 @@ | ||
| /* | ||
| * Copyright Contributors to Agones a Series of LF Projects, 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
|
|
||
| // Mock ApiProxy so the hook's internal isAgonesInstalled() call | ||
| // doesn't make real HTTP requests. | ||
| vi.mock('@kinvolk/headlamp-plugin/lib', () => ({ | ||
| ApiProxy: { | ||
| request: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| import { ApiProxy } from '@kinvolk/headlamp-plugin/lib'; | ||
| import { useAgonesInstalled } from './useAgonesInstalled'; | ||
|
|
||
| describe('useAgonesInstalled', () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should start in loading state', () => { | ||
| // Never-resolving promise keeps the hook in loading state | ||
| vi.mocked(ApiProxy.request).mockReturnValue(new Promise(() => {})); | ||
| const { result } = renderHook(() => useAgonesInstalled()); | ||
|
|
||
| expect(result.current.isAgonesInstalled).toBeNull(); | ||
| expect(result.current.isAgonesCheckLoading).toBe(true); | ||
| }); | ||
|
|
||
| it('should return isAgonesInstalled=true when Agones is detected', async () => { | ||
| vi.mocked(ApiProxy.request).mockResolvedValue({ | ||
| kind: 'APIResourceList', | ||
| resources: [{ name: 'gameservers' }], | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => useAgonesInstalled()); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isAgonesInstalled).toBe(true); | ||
| }); | ||
|
|
||
| expect(result.current.isAgonesCheckLoading).toBe(false); | ||
| }); | ||
|
|
||
| it('should return isAgonesInstalled=false when Agones is not detected', async () => { | ||
| vi.mocked(ApiProxy.request).mockRejectedValue(new Error('404 Not Found')); | ||
|
|
||
| const { result } = renderHook(() => useAgonesInstalled()); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.isAgonesInstalled).toBe(false); | ||
| }); | ||
|
|
||
| expect(result.current.isAgonesCheckLoading).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * Copyright Contributors to Agones a Series of LF Projects, 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| import { ApiProxy } from '@kinvolk/headlamp-plugin/lib'; | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| /** | ||
| * Checks whether the Agones CRDs are installed on the current cluster by | ||
| * querying the {@link https://agones.dev/site/docs/reference/agones_crd_api_reference/ | Agones API group} | ||
| * at `/apis/agones.dev/v1`. | ||
| * | ||
| * The response is validated to be a genuine Kubernetes `APIResourceList` | ||
| * (not a `Status` error object that some proxies return for 404s). | ||
| * | ||
| * @returns `true` if Agones CRDs are present, `false` otherwise. | ||
| */ | ||
| export async function isAgonesInstalled(): Promise<boolean> { | ||
| try { | ||
| const response = await ApiProxy.request('/apis/agones.dev/v1', { | ||
| method: 'GET', | ||
| }); | ||
| // Verify the response is a real K8s API resource list, not an error object. | ||
| return response?.kind === 'APIResourceList' && Array.isArray(response?.resources); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * React hook that asynchronously checks whether the Agones CRDs are installed | ||
| * on the current Kubernetes cluster. | ||
| * | ||
| * @returns An object with: | ||
| * - `isAgonesInstalled` — `null` while loading, `true` if detected, `false` if not. | ||
| * - `isAgonesCheckLoading` — `true` while the API check is in progress. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const { isAgonesInstalled, isAgonesCheckLoading } = useAgonesInstalled(); | ||
| * if (isAgonesCheckLoading) return <Spinner />; | ||
| * if (!isAgonesInstalled) return <NotInstalledBanner />; | ||
| * ``` | ||
| */ | ||
| export function useAgonesInstalled() { | ||
| const [isInstalled, setIsInstalled] = useState<boolean | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| async function checkInstalled() { | ||
| const installed = await isAgonesInstalled(); | ||
| setIsInstalled(!!installed); | ||
| } | ||
| checkInstalled(); | ||
| }, []); | ||
|
|
||
| return { | ||
| isAgonesInstalled: isInstalled, | ||
| isAgonesCheckLoading: isInstalled === null, | ||
| }; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added full TSDoc documentation for the hook with a description of the return object and a usage example👍