0

I have below code which uses DefaultAzureCredentials to get the Access token . The unit test give me token when running from Visual studio because the logged in user has access. the same code fails in devops pipeline as the agent created dynamically.

private async Task AcquireManagementAccessTokenAsync(CancellationToken cancellationToken) { var scope = _configuration.GetValue("Azure:ArmAuthScope");

        bool userManagedIdentity;
        bool.TryParse(_configuration.GetValue<string>("Azure:UseManagedIdentity"), out userManagedIdentity);

        
        AccessToken token = new AccessToken();            
        
        //using Managed Identity
        if (userManagedIdentity)
        {
            try
            {
        
                token = await new DefaultAzureCredential().GetTokenAsync(new TokenRequestContext(new[] { scope }), cancellationToken);
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.Message);
            }
        }           
       
        return token;
    }

How do I make my test code to run on devops? I want to mock this.

VMG
  • 129
  • 11

0 Answers0